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

public class MiniMapItem : MonoBehaviour
{
    public RectTransform rect;
    public Image imgBG;
    public Image iconBlkType;
    public Text txtType;

    private void OnDisable()
    {
        EventManager.Instance.Remove("TestMode", TestMode);
    }
    private void OnEnable()
    {
        blk = null;
        imgBG.sprite = spriteNot;
        imgBG.gameObject.SetActive(false);
        CHK = false;
    }

    [HideInInspector]
    public MapBlock blk;
    [HideInInspector]
    MiniMap miniMap;
    public void OnInitialized(MiniMap miniMap, MapBlock blk, bool SHOW_PATH)
    {
        EventManager.Instance.Add("TestMode", TestMode);

        this.miniMap = miniMap;
        this.blk = blk;
        this.blk.OnDropChanged += DropChanged;

        transform.SetAsLastSibling();
        imgBG.gameObject.SetActive(true);

        iconBlkType.sprite = SpriteManager.Instance.SpriteGet($"AtlasIngame", $"minimap{blk.blkType}");
        iconBlkType.gameObject.SetActive(blk.blkType != MapBlock.Type.None && blk.blkType != MapBlock.Type.Monster);
        iconBlkType.SetNativeSize();

        imgBG.enabled = false;
        iconBlkType.enabled = false;
    }

    bool TEST = false;
    void TestMode(object value)
    {
        TEST = (bool)value;

        imgBG.enabled = TEST || CHK;
        iconBlkType.enabled = TEST || CHK;
    }
    
    public Sprite spriteCurrent;
    public Sprite spriteClear;
    public Sprite spriteNot;
    public bool CHK = false;
    public void MapChanged(MapBlock blk, bool NEAR = true)
    {
        if (blk == null || this.blk == null)
            return;

        if (NEAR && blk == this.blk)
        {
            miniMap.OnNear(this.blk, this.blk.idx);
            imgBG.sprite = spriteCurrent;
        }
        else
            imgBG.sprite = this.blk.CLEAR ? spriteClear : spriteNot;

        imgBG.enabled = true;
        iconBlkType.enabled = true;
        CHK = true;

        if (iconBlkType.gameObject.activeSelf)
            iconBlkType.transform.SetAsLastSibling();
    }

    public void DropChanged(List<DropItem> list)
    {
        switch(blk.blkType)
        {
            case MapBlock.Type.Empty:
            case MapBlock.Type.Monster:
            case MapBlock.Type.None:
                {
                    if (list != null && 0 < list.Count)
                    {
                        ItemData item = list[list.Count - 1].item;
                        string type = item.type;
                        switch (type)
                        {
                            case "Use":
                            case "Pickup":
                                {
                                    switch(item.idx)
                                    {
                                        case "Heart1":
                                        case "Heart2":
                                            type = "Heart";
                                            break;
                                        case "ArmorHeart1":
                                        case "ArmorHeart2":
                                            type = "ArmorHeart";
                                            break;
                                        case "ManaPotion1":
                                        case "ManaPotion2":
                                        case "ManaPotion3":
                                            type = "ManaPotion";
                                            break;
                                        default:
                                            type = item.idx;
                                            break;
                                    }
                                }
                                break;
                        }

                        iconBlkType.sprite = SpriteManager.Instance.SpriteGet($"AtlasIngame", $"minimap{type}");
                        iconBlkType.gameObject.SetActive(true);
                        iconBlkType.SetNativeSize();
                    }
                    else
                    {
                        iconBlkType.gameObject.SetActive(false);
                    }
                }
                break;
        }
    }
}