from ....utils import path_maker, zipfolder
import os
import shutil
from .enum import *
from .filteres import *
from ..classes import Conversation
from telegram.ext import Filters


def setuper(value: str, subset: str, unit: str = '', type: str = '', hide: int = 0, toggle: int = 0):
    return {'value': value, 'subset': subset, 'unit': unit, 'type': type, 'hide': hide, 'toggle': toggle}


def backup_database(self, chat_id=''):
    path = self.db.path
    database_dir = path_maker(['database'])
    shutil.copy2(path, database_dir)
    zip_path = path_maker([], '.')+'/database.zip'
    zipfolder(zip_path, database_dir)
    with open(zip_path, 'rb') as f:
        if chat_id == '':
            chat_id = self.super_admin
        self.bot.send_document(chat_id, f)
    try:
        os.remove(zip_path)
    except:
        pass
    try:
        shutil.rmtree(database_dir)
    except:
        pass

def setup_force_join(self):
    e = Node()
    e.filters = match_btn('force_join', self)

    def act(self: Conversation, *args):
        force_joins = self.db.do('force_join')
        if force_joins:
            self.send('lists', [['add_force_join'], [self.back_menu_key]])

            for i in force_joins:
                msg = f"{self.text('title',slash=True)}: {i['title']}\n"
                msg += f"{self.text('channel_id',slash=True)}: {i['channel_id']}"
                msg += f"{self.text('link',slash=True)}: {i['link']}"
                self.send(msg, {'delete_fj': i['id']}, t=False)
        else:
            self.send('no_item', [['add_force_join'], [self.back_menu_key]])
        return -1
    e.callback = act
    self.node('force_join', [e])
    ########################################
    e = Node()
    e.pattern = self.check_inline_keyboards('delete_fj')

    def act(self: Conversation, *args):
        id = int(self.splited_query_data()[1])
        self.db.do('force_join', delete=True)
        self.edit_message_text('deleted', self.get_msg_id())
        return -1
    e.callback = act
    self.node('delete_fj', [e])
    ########################################
    e = Node()
    e.filters = match_btn('add_force_join', self)
    e.msg = 'enter_title'
    e.btn = [[self.back_menu_key]]

    s = Node()
    s.filters = Filters.text
    s.msg = 'enter_channel_id'
    s.btn = [[self.back_menu_key]]

    s1 = Node()
    s1.filters = Filters.text
    s1.msg = 'enter_channel_link'
    s1.btn = [[self.back_menu_key]]

    s2 = Node()
    s2.filters = Filters.text

    def act(self: Conversation, *args):
        title = self.userdata(self.stat_key('add_force_join', 1))['text']
        channel_id = self.userdata(self.stat_key('add_force_join', 2))['text']
        link = self.get_text()
        self.db.do('force_join', {'id': self.time(
        ), 'title': title, 'channel_id': channel_id, 'link': link, 'status': CONFIRMED})
        self.send('done', self.menu_keys)
        return -1

    s2.callback = act
    self.node('add_force_join', [e], [[s], [s1], [s2]])
