using Assets.Scripts;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Controls : MonoBehaviour
{
    GameObject player;
    GameObject fight;
    GameObject worldGen;
    GameObject playerCam;
    UIHandler uihandler;

    void Start()
    {
        player = GameObject.Find("Player");
        fight = GameObject.Find("Fight");
        worldGen = GameObject.Find("WorldGenerator");
        playerCam = GameObject.Find("InformationCamera");
        uihandler = GameObject.Find("UIHandler").GetComponent<UIHandler>();
    }

    // Update is called once per frame
    void Update()
    {
        if (!player.GetComponent<Player>().takeDamage(0))
        {
            if (!uihandler.isPlayerInFight())
            {
                checkNormalControls();
            }
            else
            {
                checkFightControls();
            }
        }
    }

    private void checkNormalControls()
    {
        if (uihandler.canPlayerMove())
        {
            player.GetComponent<Player>().move();
            if (Input.GetKeyDown(KeyCode.M))
            {
                worldGen.GetComponent<WorldGenerator>().prepareMap();
                uihandler.switchMap();
            }
            else if (Input.GetKeyDown(KeyCode.E))
            {
                GameObject target = playerCam.GetComponent<PlayerCamera>().interactWithObject();
                if (target != null)
                {
                    switch (target.tag.Split(':')[1])
                    {
                        case "Enemy":
                            fight.GetComponent<Fight>().startFight(worldGen.GetComponent<WorldGenerator>().getCurrentTile(), target, player);
                            break;
                    }
                }
            }
            else if (Input.GetKeyDown(KeyCode.C))
            {
                uihandler.switchCharactersheet();
            }
            else if (Input.GetKeyDown(KeyCode.Q))
            {
                uihandler.switchQuestLog();
            }
        }
        
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            uihandler.switchPauseMenu();
        }
    }

    private void checkFightControls()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1) || Input.GetKeyDown(KeyCode.Keypad1))
        {
            fight.GetComponent<Fight>().playerAction(1);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha2) || Input.GetKeyDown(KeyCode.Keypad2))
        {
            fight.GetComponent<Fight>().playerAction(2);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha3) || Input.GetKeyDown(KeyCode.Keypad3))
        {
            fight.GetComponent<Fight>().playerAction(3);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha4) || Input.GetKeyDown(KeyCode.Keypad4))
        {
            fight.GetComponent<Fight>().playerAction(4);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha5) || Input.GetKeyDown(KeyCode.Keypad5))
        {
            fight.GetComponent<Fight>().playerAction(5);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha6) || Input.GetKeyDown(KeyCode.Keypad6))
        {
            fight.GetComponent<Fight>().playerAction(6);
        }
    }
}
