using Games.LogicObj;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using GCGame;
using Module.Log;
using Games.GlobeDefine;
using System.Collections.Generic;
using System;
using GCGame.Table;
using Games.Events;

public class SoulTipChild : MonoBehaviour
{
    public Text Curr;
    public GameObject FirstWnd;
    public GameObject SecondWnd;
    public GameObject FirstAttrObj;

    public UIImgText FirstPower;
    public UIImgText SecondPower;

    public GameObject SecondAttrObj;
    public Text NeedStar;

    void OnEnable()
    {
        InputOutView outView = GetComponent<InputOutView>();
        if (outView != null)
        {
            outView.Add(1, false, delegate()
            {
                gameObject.SetActive(false);
            });
        }
    }

    public void InitInfo(int type,int learnNum,int HeavenID)
    {
        if(type==1)
        {
            InitLinkInfo(learnNum, HeavenID);
        }
        else
        {
            InitDeBlockInfo(learnNum, HeavenID);
        }
    }

    List<GameObject> _CloneGameObjs = new List<GameObject>();

    void InitLinkInfo(int learnNum,int HeavenID)
    {
        for(int i=0;i< _CloneGameObjs.Count;i++)
        {
            _CloneGameObjs[i].SetActive(false);
            _CloneGameObjs[i].transform.SetParent(null);
            GameObject.Destroy(_CloneGameObjs[i]);
        }
        _CloneGameObjs.Clear();

        int next = 0;
        Tab_SoulGeneralLink firstData = null;
        var links = TableManager.GetSoulGeneralLink().Values;
        foreach (var link in links)
        {
            if (link.HeavenID != HeavenID)
                continue;
            if (firstData == null)
                firstData = link;
            Tab_SoulHeavenAttr soulHeaven = TableManager.GetSoulHeavenAttrByID(link.HeavenID, 0);
            if (soulHeaven == null)
                continue;
            if (next==0 && link.StarMin <= learnNum && link.StarMax >= learnNum)
            {
                FirstWnd.SetActive(true);
                Curr.text = StrDictionary.GetClientDictionaryString("#{46003}", soulHeaven.Name, string.Format("<color=#019311ff>{0}</color>", learnNum));
                FirstPower.text = link.AttrPower.ToString();
                for (int j = 0; j < link.getAttrIdCount(); j++)
                {
                    CloneAttrObj(FirstAttrObj, link.GetAttrIdbyIndex(j), link.GetAttrValuebyIndex(j));
                }
                next++;
                continue;
            }
            if (next == 1)
            {
                SecondWnd.SetActive(true);
                SecondPower.text = link.AttrPower.ToString();
                for (int j = 0; j < link.getAttrIdCount(); j++)
                {
                    CloneAttrObj(SecondAttrObj, link.GetAttrIdbyIndex(j), link.GetAttrValuebyIndex(j));
                }
                NeedStar.text = StrDictionary.GetClientDictionaryString("#{46004}", link.StarMin);
                next++;
                break;
            }
        }

        foreach (var value in links)
        {
            if (firstData == null && value.HeavenID == HeavenID)
                firstData = value;
        }

        if (next == 1)
            SecondWnd.SetActive(false);
        if(next==0)
        {
            FirstWnd.SetActive(false);
            if(firstData!=null)
            {
                Tab_SoulHeavenAttr soulHeaven = TableManager.GetSoulHeavenAttrByID(firstData.HeavenID, 0);
                if (soulHeaven == null)
                    return;
                Curr.text = StrDictionary.GetClientDictionaryString("#{46003}", soulHeaven.Name, string.Format("<color=#019311ff>{0}</color>", learnNum));
                SecondWnd.SetActive(true);
                SecondPower.text = firstData.AttrPower.ToString();
                for (int i = 0; i < firstData.getAttrIdCount(); i++)
                {
                    CloneAttrObj(SecondAttrObj, firstData.GetAttrIdbyIndex(i), firstData.GetAttrValuebyIndex(i));
                }
                NeedStar.text = StrDictionary.GetClientDictionaryString("#{46004}", firstData.StarMin);
            }            
        }
    }

    void InitDeBlockInfo(int learnNum,int HeavenID)
    {
        for (int i = 0; i < _CloneGameObjs.Count; i++)
        {
            _CloneGameObjs[i].SetActive(false);
            _CloneGameObjs[i].transform.SetParent(null);
            GameObject.Destroy(_CloneGameObjs[i]);
        }
        _CloneGameObjs.Clear();
        int next = 0;
        Tab_SoulHeavenUnlock firstData = null;
        var links = TableManager.GetSoulHeavenUnlock().Values;
        foreach (var link in links)
        {
            if (link == null)
                continue;
            if (link.HeavenID != HeavenID)
                continue;
            if (firstData == null)
                firstData = link;
            Tab_SoulHeavenAttr soulHeaven = TableManager.GetSoulHeavenAttrByID(link.HeavenID, 0);
            if (soulHeaven == null)
                continue;
            if (next == 0 && link.StarTotalMin <= learnNum && link.StarTotalMax >= learnNum)
            {
                FirstWnd.SetActive(true);
                Curr.text = StrDictionary.GetClientDictionaryString("#{46005}", string.Format("<color=#019311ff>{0}</color>", learnNum));
                FirstPower.text = link.AttrPower.ToString();
                for (int j = 0; j < link.getAttrIdCount(); j++)
                {
                    CloneAttrObj(FirstAttrObj, link.GetAttrIdbyIndex(j), link.GetAttrValuebyIndex(j));
                }
                next++;
                continue;
            }
            if (next == 1)
            {
                SecondWnd.SetActive(true);
                SecondPower.text = link.AttrPower.ToString();
                for (int j = 0; j < link.getAttrIdCount(); j++)
                {
                    CloneAttrObj(SecondAttrObj, link.GetAttrIdbyIndex(j), link.GetAttrValuebyIndex(j));
                }
                NeedStar.text = StrDictionary.GetClientDictionaryString("#{46006}", link.StarTotalMin);
                next++;
                break;
            }
        }

        foreach (var value in links)
        {
            if (firstData == null && value.HeavenID == HeavenID)
                firstData = value;
        }

        if (next == 1)
            SecondWnd.SetActive(false);
        if (next == 0)
        {
            FirstWnd.SetActive(false);
            if (firstData != null)
            {
                SecondWnd.SetActive(true);
                SecondPower.text = firstData.AttrPower.ToString();
                Curr.text = StrDictionary.GetClientDictionaryString("#{46005}", string.Format("<color=#019311ff>{0}</color>", learnNum));
                for (int i = 0; i < firstData.getAttrIdCount(); i++)
                {
                    CloneAttrObj(SecondAttrObj, firstData.GetAttrIdbyIndex(i), firstData.GetAttrValuebyIndex(i));
                }
                NeedStar.text = StrDictionary.GetClientDictionaryString("#{46006}", firstData.StarTotalMin);
            }
        }
    }

    void CloneAttrObj(GameObject obj,int proID,int proValue)
    {
        if (proID == -1)
            return;
        GameObject newObj = GameObject.Instantiate(obj);
        newObj.SetActive(true);
        newObj.transform.SetParent(obj.transform.parent);
        newObj.transform.localPosition = obj.transform.localPosition;
        newObj.transform.localScale = obj.transform.localScale;
        Text attr = newObj.GetComponent<Text>();
        if (attr != null)
            attr.text = PropID.GetAttrName((PropID.PropertyID)proID);
        Transform child = newObj.transform.Find("Text");
        if(child!=null)
        {
            Text attrValue = child.GetComponent<Text>();
            if (attrValue != null)
                attrValue.text = proValue.ToString();
        }
        _CloneGameObjs.Add(newObj);
    }
}