# Allows users to link their Clash Of Clans account with their Discord account

import json

from Data.Modifiable_variables.import_var import Linked_accounts
from Data.utils import Utils
from Script.Clients.clash_of_clans_client import Clash_of_clans
from Script.import_functions import create_embed


async def link_coc_account(ctx, player_tag, player_token):
    is_correct_token = await Clash_of_clans.verify_player_token(player_tag, player_token)
    if is_correct_token:
        Linked_accounts[ctx.author.id].update({"Clash Of Clans": player_tag})
        embed = create_embed("Accounts linked", f"Your Discord account is now linked with the Clash Of Clans account `{player_tag}`.", ctx.guild.me.color, "Your Clash Of Clans token has been automatically regenerated by Clash Of Clans, so don't worry if people are seeing the token you just used :wink:", ctx.guild.me.avatar_url)
        await ctx.send(embed=embed)
        json_text = json.dumps(Linked_accounts, sort_keys=True, indent=4)
        def_linked_account = open(Utils["secure_folder_path"] + "linked_accounts.json", "w")
        def_linked_account.write(json_text)
        def_linked_account.close()

# Allows users to link their Clash Of Clans account with their Discord account

import json

from Data.Modifiable_variables.import_var import Linked_accounts
from Data.utils import Utils
from Script.Clients.clash_of_clans_client import Clash_of_clans
from Script.import_functions import create_embed


async def link_coc_account(ctx, player_tag, player_token):
    is_correct_token = await Clash_of_clans.verify_player_token(player_tag, player_token)
    if is_correct_token:
        Linked_accounts[ctx.author.id].update({"Clash Of Clans": player_tag})
        embed = create_embed("Accounts linked", f"Your Discord account is now linked with the Clash Of Clans account `{player_tag}`.", ctx.guild.me.color, "Your Clash Of Clans token has been automatically regenerated by Clash Of Clans, so don't worry if people are seeing the token you just used :wink:", ctx.guild.me.avatar_url)
        await ctx.send(embed=embed)
        json_text = json.dumps(Linked_accounts, sort_keys=True, indent=4)
        def_linked_account = open(Utils["secure_folder_path"] + "linked_accounts.json", "w")
        def_linked_account.write(json_text)
        def_linked_account.close()

# Allows users to link their Clash Of Clans account with their Discord account