using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System.Collections.Generic;

//Use the Selectable class as a base class to access the IsHighlighted method
public class SetVarToDrawDescription : MonoBehaviour
{

    public GameObject obj; // ссылка на создаваемый вирус
    private Controller controller;
    private DrawAbilityDescription drawDescription, drawDescriptionHotbar;
    private Image image; // для затемнения кнопки при наведении

    private GameObject virus, parent;
    public GameObject selfButton,sameButton, infoSelf, infoSame;
    public bool status;
    private void Awake() 
    {
        parent = transform.parent.gameObject; // находим предка
        controller = GameObject.Find("Controller").GetComponent<Controller>();
        drawDescription = parent.GetComponent<DrawAbilityDescription>();
        image = GetComponent<Image>();
        drawDescriptionHotbar = GameObject.Find("HotBar").GetComponent<DrawAbilityDescription>();

        if (status == false) {controller.SetActiveButton(selfButton, infoSelf, false);}
    }
    void OnMouseEnter() 
    {
        drawDescription.DrawInfo(true, gameObject);
        transform.localScale = new Vector2(1.1f,1.1f);
        image.color = new Color32(180,180,180,255);
        
        
    }
    void OnMouseExit() 
    {
        drawDescription.DrawInfo(false);
        transform.localScale = new Vector2(1f,1f);
        image.color = new Color32(255,255,255,255);
    }
    
    void OnMouseDown() 
    {
        // нажатие кнопок хотбара
        switch(parent.name)
        {
            ///// НАЖАТИЕ КНОПОК ХОТБАРА
            case "HotBar":
                if (status == false)
                {
                    controller.DrawMessage("Еще не изучено!!!");
                }
                else
                {
                    
                    if (this.name == "Development")
                    {
                        // открытие меню прокачки
                        controller.research.SetActive(true);
                        controller.hotbar.SetActive(false);
                    }
                    else
                    {
                        

                        if (controller.coin >= controller.costAbilities[this.name])
                        {
                            controller.ChangeCoin( - controller.costAbilities[this.name]);

                            if (this.name == "Scan")
                            {
                            // Запуск сканирования
                                
                            }
                            else
                            {         
                                virus = Instantiate(obj,GameObject.Find("Covid").transform.position, Quaternion.identity);
                                virus.GetComponent<VirusMoving>().ability = this.name;
                                
                                if (this.name == "Ability02") 
                                {
                                    virus.GetComponent<VirusMoving>().impact = controller.impactAbility["Ability01"];
                                    virus.GetComponent<VirusMoving>().time = controller.impactAbility[this.name];
                                }
                                else
                                {virus.GetComponent<VirusMoving>().impact = controller.impactAbility[this.name];}
                            }

                            if (this.name == "Ability04" || this.name == "Ability06" || this.name == "Scan")
                                {
                                    status = false;
                                    controller.SetActiveButton(selfButton, infoSelf, false);

                                    sameButton.GetComponent<SetVarToDrawDescription>().status = true;
                                    controller.SetActiveButton(sameButton, infoSame, true);
                                }
                        }
                        else
                        {
                            controller.DrawMessage("Недостаточно игдкойнов!!!");
                        }
                    }
                }                
            break;

            ///// НАЖАТИЕ КНОПОК МАГАЗИНА
            case "Research":
                if (this.name == "Cancel")
                {
                    controller.hotbar.SetActive(true);
                    controller.research.SetActive(false);
                }
                else
                {

                    switch(this.name)
                    {
                        case "Ability01":
                            if (this.status && controller.coin >= controller.costUpAbility[this.name])
                            {
                                controller.ChangeCoin( - controller.costUpAbility[this.name]);
                                controller.ChangeValues(controller.impactAbility,this.name, controller.impactAbility[this.name] * controller.qImpact[this.name]);
                                controller.ChangeValues(controller.costAbilities, this.name, controller.costAbilities[this.name] * controller.qCost[this.name], drawDescriptionHotbar.textCost01, true);
                                controller.ChangeValues(controller.costUpAbility, this.name, controller.costUpAbility[this.name] * controller.qCostUp[this.name], drawDescription.textCost01, true);

                                controller.ChangeValues(controller.costAbilities, "Ability02", controller.costAbilities[this.name] * controller.qCost["Ability02"], drawDescriptionHotbar.textCost02, true);
                            }
                            else
                            {controller.DrawMessage("Недостаточно игдкойнов!!!");}
                        break;

                        case "Ability02":
                            if (this.status && controller.coin >= controller.costUpAbility[this.name])
                            {
                                controller.ChangeCoin( - controller.costUpAbility[this.name]);

                                if (sameButton.GetComponent<SetVarToDrawDescription>().status)
                                {
                                    controller.ChangeValues(controller.impactAbility,this.name,controller.impactAbility[this.name] * controller.qImpact[this.name]);
                                    controller.ChangeValues(controller.costAbilities, this.name, controller.costAbilities["Ability01"] * controller.qCost[this.name], drawDescriptionHotbar.textCost02, true);
                                }
                                else
                                {
                                    controller.ChangeValues(controller.impactAbility,this.name,controller.impactAbility[this.name]);
                                    controller.ChangeValues(controller.costAbilities, this.name, controller.costAbilities["Ability01"] * controller.qCost[this.name], drawDescriptionHotbar.textCost02, true);
                                    controller.SetActiveButton(sameButton, infoSame, true);
                                    sameButton.GetComponent<SetVarToDrawDescription>().status = true;
                                }
                                controller.ChangeValues(controller.costUpAbility, this.name, controller.costUpAbility[this.name] * controller.qCostUp[this.name], drawDescription.textCost02, true);
                            }
                            else
                            {controller.DrawMessage("Недостаточно игдкойнов!!!");}
                        break;

                        case "Ability03":
                            if (this.status && controller.coin >= controller.costUpAbility[this.name])
                            {
                                controller.ChangeCoin( - controller.costUpAbility[this.name]);

                                if (sameButton.GetComponent<SetVarToDrawDescription>().status)
                                {
                                    controller.ChangeValues(controller.impactAbility,this.name,controller.impactAbility[this.name] * controller.qImpact[this.name]);
                                    controller.ChangeValues(controller.costAbilities, this.name, controller.costAbilities[this.name] * controller.qCost[this.name], drawDescriptionHotbar.textCost03, true);
                                }
                                else
                                {
                                    controller.ChangeValues(controller.impactAbility,this.name,controller.impactAbility[this.name]);
                                    controller.ChangeValues(controller.costAbilities, this.name, controller.costAbilities[this.name], drawDescriptionHotbar.textCost03, true);
                                    controller.SetActiveButton(sameButton, infoSame, true);
                                    sameButton.GetComponent<SetVarToDrawDescription>().status = true;
                                }
                                controller.ChangeValues(controller.costUpAbility, this.name, controller.costUpAbility[this.name] * controller.qCostUp[this.name], drawDescription.textCost03, true);

                            }
                            else
                            {controller.DrawMessage("Недостаточно игдкойнов!!!");} 
                        break;

                        case "Ability04":
                            if (sameButton.GetComponent<SetVarToDrawDescription>().status == false)
                            {
                                if (this.status && controller.coin >= controller.costUpAbility[this.name])
                                {
                                    controller.ChangeCoin( - controller.costUpAbility[this.name]);
                                    controller.ChangeValues(controller.costAbilities, this.name, controller.costAbilities[this.name] * controller.qCost[this.name], drawDescriptionHotbar.textCost04, true);
                                    controller.ChangeValues(controller.costUpAbility, this.name, controller.costUpAbility[this.name] * controller.qCostUp[this.name], drawDescription.textCost04, true);

                                    controller.SetActiveButton(sameButton, infoSame, true);
                                    sameButton.GetComponent<SetVarToDrawDescription>().status = true;
                                    status = false;
                                    controller.SetActiveButton(selfButton, infoSelf, status);
                                }
                                else
                                {controller.DrawMessage("Недостаточно игдкойнов!!!");}
                            }
                            else
                            {controller.DrawMessage("Уже изучено!!!");}
                        break;

                        case "Ability05":
                            if (this.status && controller.coin >= controller.costUpAbility[this.name])
                            {
                                controller.ChangeCoin( - controller.costUpAbility[this.name]);

                                if (sameButton.GetComponent<SetVarToDrawDescription>().status)
                                {
                                    controller.ChangeValues(controller.impactAbility,this.name,controller.impactAbility[this.name] * controller.qImpact[this.name]);
                                    controller.ChangeValues(controller.costAbilities, this.name, controller.costAbilities[this.name] * controller.qCost[this.name], drawDescriptionHotbar.textCost05, true);
                                }
                                else
                                {
                                    controller.ChangeValues(controller.impactAbility,this.name,controller.impactAbility[this.name]);
                                    controller.ChangeValues(controller.costAbilities, this.name, controller.costAbilities[this.name], drawDescriptionHotbar.textCost05, true);
                                    controller.SetActiveButton(sameButton, infoSame, true);
                                    sameButton.GetComponent<SetVarToDrawDescription>().status = true;
                                }
                                controller.ChangeValues(controller.costUpAbility, this.name, controller.costUpAbility[this.name] * controller.qCostUp[this.name], drawDescription.textCost05, true);

                            }
                            else
                            {controller.DrawMessage("Недостаточно игдкойнов!!!");}
                        break;

                        case "Ability06":
                            if (sameButton.GetComponent<SetVarToDrawDescription>().status == false)
                            {
                                if (this.status && controller.coin >= controller.costUpAbility[this.name])
                                {
                                    controller.ChangeCoin( - controller.costUpAbility[this.name]);
                                    controller.ChangeValues(controller.impactAbility,this.name, controller.impactAbility[this.name] * controller.qImpact[this.name]);
                                    controller.ChangeValues(controller.costAbilities, this.name, controller.costAbilities[this.name] * controller.qCost[this.name], drawDescriptionHotbar.textCost06, true);
                                    controller.ChangeValues(controller.costUpAbility, this.name, controller.costUpAbility[this.name] * controller.qCostUp[this.name], drawDescription.textCost06, true);

                                    controller.SetActiveButton(sameButton, infoSame, true);
                                    sameButton.GetComponent<SetVarToDrawDescription>().status = true;
                                    status = false;
                                    controller.SetActiveButton(selfButton, infoSelf, status);
                                }
                                else
                                {controller.DrawMessage("Недостаточно игдкойнов!!!");}
                            }
                            else
                            {controller.DrawMessage("Уже изучено!!!");}
                        break;

                        case "Scan":
                            if (sameButton.GetComponent<SetVarToDrawDescription>().status == false)
                            {
                                if (this.status && controller.coin >= controller.costUpAbility[this.name])
                                {
                                    controller.ChangeCoin( - controller.costUpAbility[this.name]);
                                    controller.ChangeValues(controller.costAbilities, this.name, controller.costAbilities[this.name] * controller.qCost[this.name], drawDescriptionHotbar.textCost07, true);
                                    controller.ChangeValues(controller.costUpAbility, this.name, controller.costUpAbility[this.name] * controller.qCostUp[this.name], drawDescription.textCost07, true);

                                    controller.SetActiveButton(sameButton, infoSame, true);
                                    sameButton.GetComponent<SetVarToDrawDescription>().status = true;
                                    status = false;
                                    controller.SetActiveButton(selfButton, infoSelf, status);
                                }
                                else
                                {controller.DrawMessage("Недостаточно игдкойнов!!!");}
                            }
                            else
                            {controller.DrawMessage("Уже изучено!!!");}
                        break;

                        case "Income":
                            if (this.status && controller.coin >= controller.costUpAbility[this.name])
                            {
                                controller.ChangeCoin( - controller.costUpAbility[this.name]);
                                controller.ChangeValues(controller.impactAbility, this.name, controller.impactAbility[this.name] * controller.qImpact[this.name], null, true);
                                controller.coinGain = controller.impactAbility[this.name];
                                controller.ChangeValues(controller.costUpAbility,this.name,controller.costUpAbility[this.name] * controller.qCostUp[this.name],drawDescription.textCost08, true);
                            }
                            else
                            {controller.DrawMessage("Недостаточно игдкойнов!!!");}
                        break;
                    }

                    
                }
            break;
        }
        if (this.name != "Income" && this.name != "Cancel" && this.name != "Development")
        {
            drawDescription.DrawInfo(true, gameObject);
            drawDescriptionHotbar.DrawInfo(true, gameObject);
        }
        else
        {drawDescription.DrawInfo(true, gameObject);}
    }
}