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

public class InventoryManager : SingletonComponent<InventoryManager>
{

    public Camera uiCam;
    public AudioSource audioSource;
    public AudioClip uiClickClip;
    public AudioClip objClickClip;

    [Space(10)]
    public LayerMask itemLayer;

    public InvenUI_Slot uiSlotPrefab;
    public InvenUI_Slot uiSelectSlot;
    public ItemInfo selectItemInfo => uiSelectSlot.slot == null || uiSelectSlot.slot.itemInfo == null ? null : uiSelectSlot.slot.itemInfo;
    //public InvenSlot selectSlot;
    public ItemKind nowClothes;

    public ItemInfo[] AllItem;
    public InvenParent clothesParent;
    public InvenUI_Parent uiClothesPopup;
    public InvenParent invenParent;
    public InvenUI_Parent uiInvenPopup;


    public GameObject wallToilet;
    public GameObject wallCook;
    public GameObject wallHide;
    public ClickEventer[] toiletClick;
    public ItemInfo baseClothes;


    private void Awake()
    {

        nowClothes = ItemKind.C_Nomarl;

     

        uiSlotPrefab.gameObject.SetActive(false);

        for (int i = 0; i < 4; i++)
        {
            invenParent.AddEmpty();
            clothesParent.AddEmpty();
        }

        //for (int i = 0; i < AllItem.Length; i++)
        //{
        //    EventAddItem(AllItem[i]);
        //}

        EventAddItem(baseClothes);
        changeCloths(ItemKind.C_Nomarl);
        uiInvenPopup.inven = invenParent;
        uiInvenPopup.uiUpdate();

        uiClothesPopup.inven = clothesParent;
        uiClothesPopup.uiUpdate();

        uiSelectSlot.gameObject.SetActive(false);
        uiSelectSlot.setSlot(null);
    }

    public void changeCloths(ItemKind clothes)
    {
        nowClothes = clothes;
        for (int i = 0; i < toiletClick.Length; i++)
        {
            toiletClick[i].eventEnableToggle(false);
        }
        wallCook.gameObject.SetActive(true);
        wallHide.gameObject.SetActive(true);
        wallToilet.gameObject.SetActive(true);
        switch (clothes)
        {
            case ItemKind.C_Glasses:
                Player.Instance.ChageClothes(4);
                break;
            case ItemKind.C_Woman:
                Player.Instance.ChageClothes(3);
                //화장실 벽이 열립니다.
                wallToilet.gameObject.SetActive(false);
                for (int i = 0; i < toiletClick.Length; i++)
                {
                    toiletClick[i].eventEnableToggle(true);
                }
                break;
            case ItemKind.C_Chef:
                Player.Instance.ChageClothes(2);
                wallCook.gameObject.SetActive(false);
                //주방 벽이 열립니다.
                break;
            case ItemKind.C_Guard:
                Player.Instance.ChageClothes(1);
                wallHide.gameObject.SetActive(false);
                //지하 벽이 열립니다.
                break;
            case ItemKind.C_Nomarl:
                Player.Instance.ChageClothes(0);
                break;
        }
    }
    public void selectSlotUse()
    {
        if (this.uiSelectSlot.slot != null && this.uiSelectSlot.slot.itemInfo != null)
        {
            invenParent.RemoveItem(this.uiSelectSlot.slot.itemInfo);
        }
    }
    public void selectSlotClick(InvenSlot clickSlot)
    {
        if (this.uiSelectSlot.slot != null)
        {
            selectSlotEnd(clickSlot);
        }
        else
        {
            if (clickSlot == null)
            {
                return;
            }
            this.uiSelectSlot.setSlot(clickSlot);
            this.uiSelectSlot.gameObject.SetActive(true);
        }
    }

    public void selectSlotEnd(InvenSlot dropSlot)
    {
        if (uiSelectSlot.slot == null)
        {
            return;
        }
        //아이템 위치를 바꿀 필요는 없음
        if (dropSlot != null && uiSelectSlot.slot != dropSlot && uiSelectSlot.slot.itemInfo != null && dropSlot.itemInfo)
        {
            //상호작용 있는지 체크
            //조합이 있는지 체크해서 조합할것
            switch (uiSelectSlot.slot.itemInfo.itemKind)
            {
                case ItemKind.C_Glasses:
                    break;
                case ItemKind.C_Woman:
                    break;
                case ItemKind.C_Chef:
                    break;
                case ItemKind.C_Guard:
                    break;
                case ItemKind.C_Nomarl:
                    break;
                case ItemKind.None:
                    break;
                case ItemKind.Cocktail:
                    break;
                case ItemKind.Powder:
                    break;
                case ItemKind.GlassSlipper:
                    break;
                case ItemKind.Clipper:
                    break;
                case ItemKind.Toy:
                    break;
                case ItemKind.SilverSpoon:
                    break;
                case ItemKind.Hellmet:
                    if (dropSlot.itemInfo.itemKind == ItemKind.MopHead)
                    {
                        invenParent.RemoveItem(dropSlot.itemInfo);
                        invenParent.RemoveItem(uiSelectSlot.slot.itemInfo);
                        for (int i = 0; i < AllItem.Length; i++)
                        {
                            if (AllItem[i].itemKind == ItemKind.GeneralHead)
                            {
                                invenParent.AddItem(AllItem[i]);
                                break;
                            }
                        }
                    }
                    break;
                case ItemKind.MopHead:
                    if (dropSlot.itemInfo.itemKind == ItemKind.Hellmet)
                    {
                        invenParent.RemoveItem(dropSlot.itemInfo);
                        invenParent.RemoveItem(uiSelectSlot.slot.itemInfo);
                        for (int i = 0; i < AllItem.Length; i++)
                        {
                            if (AllItem[i].itemKind == ItemKind.GeneralHead)
                            {
                                invenParent.AddItem(AllItem[i]);
                                break;
                            }
                        }
                    }
                    break;
                case ItemKind.GeneralHead:
                    break;
                case ItemKind.Dishcloth:
                    break;
                case ItemKind.Ranmen:
                    break;
                case ItemKind.Dumbbell:
                    break;
                case ItemKind.MopStick:
                    break;
                case ItemKind.PrisonKey:
                    break;
                case ItemKind.Candy:
                    break;
                case ItemKind.PoisonFood:
                    break;
                case ItemKind.Drass:
                    break;
                case ItemKind.Memo:
                    break;
                default:
                    break;
            }
            invenParent.RemoveEmpty();
            uiInvenPopup.uiUpdate();

        }
        this.uiSelectSlot.setSlot(null);
        this.uiSelectSlot.gameObject.SetActive(false);
    }

    public void Update()
    {
        if (uiSelectSlot.slot != null && uiSelectSlot.slot.itemInfo != null)
        {
            Vector3 mousePos = uiCam.ScreenToWorldPoint(Input.mousePosition);
            mousePos.z = uiSelectSlot.Rect.position.z;
            uiSelectSlot.Rect.position = mousePos;
            if (Input.GetMouseButtonDown(1))
            {
                //오른쪽 클릭
                selectSlotEnd(null);
            }
        }

        //오브젝트 상호작용
        if (Input.GetMouseButtonDown(0))//클릭    
        {        //마우스 포인터가 UI위에 있지않으면       
            if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject() == true)
            {
                audioSource.Pause();
                audioSource.clip = uiClickClip;
                audioSource.time = 0f;
                audioSource.Play();
                //UI 클릭했네
                return;
            }

            Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero, 100f, itemLayer);
            if (hit)
            {
                ClickEventer clickEventer = hit.transform.GetComponent<ClickEventer>();
                if (clickEventer != null)
                {
                    clickEventer.OnClickEvent();
                    selectSlotEnd(null);

                    audioSource.Pause();
                    audioSource.clip = objClickClip;
                    audioSource.time = 0f;
                    audioSource.Play();
                }
            }
        }
    }

    public void EventAddItem(ItemInfo itemInfo)
    {
        switch (itemInfo.itemType)
        {
            case ItemType.Item:
                invenParent.AddItem(itemInfo);
                uiInvenPopup.uiUpdate();
                break;
            case ItemType.Clothes:
                clothesParent.AddItem(itemInfo);
                uiClothesPopup.uiUpdate();
                break;
        }
    }
}
