using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GCGame.Table;
using Module.Log;
using System;

// 任务用的物品获取列表
// 和 ItemGetPathPopRoot 仅在一些表现上有区别,所以逻辑和枚举基本沿用 ItemGetPathPopRoot 的配置
public class MissionItemGetPath : MonoBehaviour {

    private static MissionItemGetPath instance;
    public static MissionItemGetPath Instance
    {
        get { return instance; }
    }

    public class MissionGetPathData : ItemGetPathPopRoot.GetPathData
    {
        public string btnDesc;
    }

    public UIBackRayBehind bgMask;
    public RectTransform anchor;
    public UIContainerBase container;

    public static void Show(int tabID, Vector2 worldPos)
    {
        Hashtable tempParam = new Hashtable();
        tempParam["ID"] = tabID;
        tempParam["Pos"] = worldPos;
        UIManager.ShowUI(UIInfo.MissionItemGetPath,
            (bool isSuccess, object param) =>
            {
                if (isSuccess)
                {
                    MissionItemGetPath.Instance.ShowGetPathByID((int)((Hashtable)param)["ID"], (Vector2)((Hashtable)param)["Pos"]);
                }
            }, tempParam);
    }

    private void Awake()
    {
        if(instance == null)
        {
            instance = this;
            bgMask._BackClick.AddListener(Close);
        }
    }

    public void ShowGetPathByID(int tabID, Vector2 worldPos)
    {
        Tab_MissionItemGetPath tab = TableManager.GetMissionItemGetPathByID(tabID, 0);
        if(tab == null)
        {
            LogModule.ErrorLog("no this id:{0} in MissionItemGetPath", tabID);
            return;
        }

        SetPos(worldPos);
        List<MissionGetPathData> itemGetPaths = new List<MissionGetPathData>();
        int count = tab.getPathTypeCount();
        for(int i = 0; i < count; ++i)
        {
            if(tab.GetPathTypebyIndex(i) != -1)
            {
                MissionGetPathData itemGetPath = new MissionGetPathData();
                itemGetPath.pathType = tab.GetPathTypebyIndex(i);
                itemGetPath.info = tab.GetPathParambyIndex(i).Trim('"');
                itemGetPath.btnDesc = tab.GetPathStrbyIndex(i).Trim('"');
                itemGetPath.tabType = 1;
                itemGetPath.itemID = tab.ItemID;
                itemGetPaths.Add(itemGetPath);
            }
            else
            {
                break;
            }
        }

        if(itemGetPaths.Count > 0)
        {
            container.InitContentItem(itemGetPaths, OnItemClick);
        }
        else
        {
            LogModule.ErrorLog("no data id:{0} in MissionItemGetPath", tabID);
            Close();
        }
    }

    private void SetPos(Vector2 worldPos)
    {
        anchor.position = worldPos;

        float newY;
        float newX;

        // 优先在右显示
        if (Screen.width - anchor.anchoredPosition.x >= anchor.rect.width)
        {
            newX = anchor.anchoredPosition.x + anchor.rect.width / 2;
        }
        else if (anchor.anchoredPosition.x >= anchor.rect.width)
        {
            newX = anchor.anchoredPosition.x - anchor.rect.width / 2;
        }
        else
        {
            newX = Screen.width - anchor.rect.width / 2;
        }

        if (Screen.height - anchor.anchoredPosition.y < anchor.rect.height / 2)
        {
            newY = Screen.height - anchor.rect.height / 2;
        }
        else if (anchor.anchoredPosition.y <= anchor.rect.height / 2)
        {
            newY = anchor.rect.height / 2;
        }
        else
        {
            newY = anchor.anchoredPosition.y;
        }

        anchor.anchoredPosition = new Vector2(newX, newY);
    }

    private void OnItemClick(object data)
    {
        MissionGetPathData info = data as MissionGetPathData;
        if(data != null)
        {
            ItemGetPathPopRoot.GoGetPath(info);
        }

        Close();
    }

    private void Close()
    {
        UIManager.CloseUI(UIInfo.MissionItemGetPath);
    }
}