from .command import fun_undercommand1
from .command import fun_undercommand2
from .command import fun_undercommand3
from .command import log

import discord


import json

def to_t (string_dir) :
    file = open(string_dir, encoding= 'utf-8', mode= 'r')
    out = file.read()
    file.close()
    return str(out)


async def fun_prefix(message, string_dir, string_generalcommand, int_j) :
    string_dir += '\\prefix.txt'
    (string_undercommand1, int_j) = fun_undercommand1(message, string_generalcommand, int_j)

    if string_undercommand1 == 'change' :
        (string_undercommand2, int_j) = fun_undercommand2(message, string_generalcommand, string_undercommand1, int_j)

        if string_undercommand2 :
            fun_change_prefix(string_dir, string_prefix= string_undercommand2)
        else:
            await message.channel.send('```Префикс мне укажи```')
            return

        await message.channel.send('```Префикс был изменён на \'%s\' ```' % string_undercommand2)

    elif string_undercommand1 == 'show' : 
        await message.channel.send('```' + to_t(string_dir) + '```')


def fun_change_prefix (string_dir, string_prefix) :
    file = open(string_dir, encoding= 'utf-8', mode= 'w')
    file.write(string_prefix)
    file.close
    return True


async def fun_show_prefix(message, string_dir, client) :
    NEW_DIR = string_dir + '\\s_' + str(message.guild.id)
    file = open(NEW_DIR + '\\prefix.txt', encoding= 'utf-8', mode= 'r')
    out = file.read()
    await message.channel.send('```Prefix: %s ```' %out)
    file.close()
    return True



async def fun_command(message, string_generalcommand, string_dir, int_j) :
    string_dir += '\\command.json'
    (string_undercommand1, int_j) = fun_undercommand1(message, string_generalcommand, int_j)

    file = open(string_dir, encoding= 'utf-8', mode= 'r')
    dir = json.load(file)
    file.close()

    if string_undercommand1 == 'change' :
        (string_undercommand2, int_j) = fun_undercommand2(message, string_generalcommand, string_undercommand1, int_j)

        if string_undercommand2 == 'play' or string_undercommand2 == dir['play'] :
            (string_undercommand3, int_j) = fun_undercommand3(message, string_generalcommand, string_undercommand1,string_undercommand2, int_j)
            if string_undercommand3 and string_undercommand3 != '\0' : 
                dir['play'] = string_undercommand3
                file = open(string_dir, encoding= 'utf-8', mode= 'w')
                json.dump(dir, file, ensure_ascii=False)
                file.close()

                log(message, '[log] Команду изменили (%s/\"play\" -> %s) ' % (string_undercommand2, string_undercommand3))
                await message.channel.send('```Команда была изменена (%s/\"play\" -> %s) ```' % (string_undercommand2, string_undercommand3))
                return True

    elif string_undercommand1 == 'show' :
        await fun_show_commands(message, string_dir)



async def fun_change_command(message, string_generalcommand, string_dir, int_j) :
    (string_undercommand1, int_j) = fun_undercommand1(message, string_generalcommand, int_j)
    
    file = open(string_dir, encoding= 'utf-8', mode= 'r')
    dir = json.load(file)
    file.close()

    if string_undercommand1 == 'play' or string_undercommand1 == dir['p'] :
        (string_undercommand2, int_j) = fun_undercommand2(message, string_generalcommand, string_undercommand1, int_j)
        if string_undercommand2 and string_undercommand2 != '\0' : 
            dir['play'] = string_undercommand2
            file = open(string_dir, encoding= 'utf-8', mode= 'w')
            json.dump(dir, file, ensure_ascii=False)
            file.close()


    await message.channel.send('```Команда была изменена на \'%s\' ```' % string_undercommand2)
    return True


async def fun_show_commands(message, string_dir) :
    file = open(string_dir, encoding= 'utf-8', mode= 'r')
    dir = json.load(file)
    file.close()
    string_out = '```\n'
    string_out += '\"play\" : ' + dir['play'] + '\n' 
    string_out += '\"help\" : ' + dir['help'] + '\n'
    string_out += '```'
    #await message.channel.send(str(dir))

    await message.channel.send(string_out)