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

public class MagicTooltipsInfo : MonoBehaviour
{
    public Text ItemName;
    public CommonItemTipsSlot commonItem;
    public Text ItemLevel;

    public Image QuilityBack;

    public GameObject[] _EquipBaseAttr;
    public Text[] _EquipBaseAttrValue;
    public GameObject _EquipExLable;
    public GameObject[] _EquipExAttr;
    public Text[] _EquipExAttrValue;
    public Image[] _EquipExAttrImage;
    public Text _EquipXilianPoint;
    public GameObject _EquipXilianObj;

    public Text DressInfo;

    public Text[] DressDescs;

    public float _ScollHeight;
    public ScrollRect _ScollRect;
    public LayoutElement _ScollLayout;
    public RectTransform _ScollContentTran;
    public GameObject _ScollTips;
    public Text PowerText;

    GameItem m_gameItem;

    public string GetMagicSubName(int subClass)
    {
        switch (subClass)
        {
            case 1://金
                return StrDictionary.GetClientDictionaryString("#{6368}");
            case 2://木
                return StrDictionary.GetClientDictionaryString("#{6369}");
            case 3://水
                return StrDictionary.GetClientDictionaryString("#{6370}");
            case 4://火
                return StrDictionary.GetClientDictionaryString("#{6371}");
            case 5://土
                return StrDictionary.GetClientDictionaryString("#{6372}");
        }
        return "";
    }

    public void SetMagicInfo(GameItem gameItem)
    {
        m_gameItem = gameItem;
        Tab_CommonItem ItemInfo = TableManager.GetCommonItemByID(gameItem.DataID, 0);
        if (ItemInfo == null)
            return;
        SetItemName();
        string pro = SetProfession();
        string level = SetItemUseLevel();
        ItemLevel.text = level + "   "+ GetMagicSubName(ItemInfo.SubClassID)+"   " + pro;
        commonItem.InitItem(gameItem);

        LoadAssetBundle.Instance.SetImageSprite(QuilityBack, Utils.GetItemQualityBG(ItemInfo.Quality));
        SetEquipAttr();

        SetScollRect();
        //_ScollContentTran.anchoredPosition = Vector2.zero;
    }
    void SetItemName()
    {
        if (m_gameItem != null && m_gameItem.IsValid())
        {
            int nQuality = (int)m_gameItem.GetQuality();
            ItemName.text = Utils.GetQualityColorInTip(nQuality);
            ItemName.text += TableManager.GetCommonItemByID(m_gameItem.DataID, 0).Name + "</color>";
        }
    }
    string SetProfession()
    {
        if (m_gameItem != null && m_gameItem.IsValid())
        {
            Tab_CommonItem tabItem = TableManager.GetCommonItemByID(m_gameItem.DataID, 0);
            if (tabItem != null)
            {
                int proLimit = tabItem.ProfessionRequire;
                string proStr = "";
                if (proLimit > 0)
                {
                    for (int i = 0; i < (int)CharacterDefine.PROFESSION.MAX; ++i)
                    {
                        if (((proLimit >> i) & 1) > 0)
                        {
                            proStr += Utils.GetProfession(i) + " ";
                        }
                    }

                    if (((proLimit >> GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Profession) & 1) == 0)
                    {
                        proStr = StrDictionary.GetClientDictionaryString("#{5526}") + proStr + "</Color>";
                    }
                }
                else
                {
                    proStr += StrDictionary.GetClientDictionaryString("#{5377}");
                }

                return proStr;
            }
        }
        return "";
    }

    string SetItemUseLevel()
    {
        if (m_gameItem != null && m_gameItem.IsValid())
        {
            int nPlayerLevel = GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Level;
            Tab_CommonItem tabItem = TableManager.GetCommonItemByID(m_gameItem.DataID, 0);
            if (null != tabItem)
            {
                int equipLevel = tabItem.MinLevelRequire;
                int nItemUseLevel = m_gameItem.GetMinLevelRequire();
                string showLevel = nItemUseLevel.ToString();
                if (nItemUseLevel < equipLevel)
                {
                    showLevel = equipLevel.ToString() + "-" + (equipLevel - nItemUseLevel).ToString();
                }
                if (nPlayerLevel >= nItemUseLevel)
                {
                    return StrDictionary.GetClientDictionaryString("#{3362}", showLevel);
                }
                else
                {
                    return StrDictionary.GetClientDictionaryString("#{5526}") + StrDictionary.GetClientDictionaryString("#{3362}", showLevel) + "</color>";
                }

            }
        }
        return "";
    }

    void SetEquipAttr()
    {
        SetBaseAttr();
        SetExAttr();
        SetDressAttr();
    }

    void Update()
    {
        SetScollRect();
    }

    public void SetBaseAttr()
    {
        Tab_MagicWeaponBaseAttr tabItem = TableManager.GetMagicWeaponBaseAttrByID(m_gameItem.DataID, 0);
        if (tabItem == null)
            return;
        var strength = m_gameItem.GetTotalStrengthVal();
        for (int i = 0; i < _EquipBaseAttr.Length; ++i)
        {
            if (tabItem.getPropIdCount() <= i || tabItem.GetPropIdbyIndex(i) < 0)
            {
                _EquipBaseAttr[i].SetActive(false);
                continue;
            }
            _EquipBaseAttr[i].SetActive(true);
            _EquipBaseAttrValue[i].text = PropID.GetAttrValue((PropID.PropertyID)tabItem.GetPropIdbyIndex(i), tabItem.GetPropSubIdbyIndex(i), tabItem.GetPropValbyIndex(i)).Replace('+', ' ');
        }
        if(m_gameItem.EnchanceScore <= 0)
        {
            PowerText.text = tabItem.CombatValue.ToString();
        }
        else
        {
            PowerText.text = m_gameItem.EnchanceScore.ToString();
        }
    }

    public void SetExAttr()
    {
        if (m_gameItem.ExAttrs.Count == 0)
        {
            _EquipExLable.SetActive(false);
        }
        else
        {
            _EquipExLable.SetActive(true);
        }

        for (int i = 0; i < _EquipExAttr.Length; ++i)
        {
            if (m_gameItem.ExAttrs.Count <= i)
            {
                _EquipExAttr[i].SetActive(false);
                continue;
            }
            if (m_gameItem.ExAttrs[i].XilianValue <= 0)
            {
                _EquipExAttr[i].SetActive(false);
                continue;
            }

            _EquipExAttr[i].SetActive(true);

            string attrStr = PropID.GetAttrValue((PropID.PropertyID)m_gameItem.ExAttrs[i].mPropID, m_gameItem.ExAttrs[i].PropSubID, m_gameItem.ExAttrs[i].XilianValue);
            _EquipExAttrValue[i].text = m_gameItem.GetEquipColorStr(true) + attrStr + "</color>";
        }

        if (m_gameItem.XilianPoint > 0)
        {
            _EquipXilianObj.SetActive(true);
            _EquipXilianPoint.text = m_gameItem.XilianPoint.ToString();
        }
        else
        {
            _EquipXilianObj.SetActive(false);
        }
    }

    public void SetDressAttr()
    {
        for(int i=1;i< DressDescs.Length;i++)
        {
            DressDescs[i].gameObject.SetActive(false);
        }
        Tab_MagicWeaponBaseAttr magicBase = TableManager.GetMagicWeaponBaseAttrByID(m_gameItem.DataID, 0);
        if (magicBase == null)
            return;
        Tab_MagicWeaponDressAttr magicDress = TableManager.GetMagicWeaponDressAttrByID(magicBase.DressID, 0);
        if (magicDress == null)
            return;
        Tab_CombatMagicWeaponSuit tab_CombatMagicWeaponSuit = TableManager.GetCombatMagicWeaponSuitByID(magicBase.DressID, 0);
        if (tab_CombatMagicWeaponSuit == null)
            return;
        int DressNum = 0;
        
        for (int i = 0; i < magicDress.getMagicWeaponIdCount(); i++)
        {
            GameItem magicItem = GameManager.gameManager.PlayerDataPool.MagicPack.GetItemByDataID(magicDress.GetMagicWeaponIdbyIndex(i));
            if (magicItem != null && magicItem.IsValid())
                DressNum++;
        }

        int NextDressNum = 0;

        for (int i = 0; i < magicDress.getReplaceIdCount(); i++)
        {
            Tab_MagicWeaponDressAttr RemagicDress = TableManager.GetMagicWeaponDressAttrByID(magicDress.GetReplaceIdbyIndex(i), 0);
            if (RemagicDress == null)
                continue;
            for(int j=0;j<RemagicDress.getMagicWeaponIdCount();j++)
            {
                GameItem magicItem = GameManager.gameManager.PlayerDataPool.MagicPack.GetItemByDataID(RemagicDress.GetMagicWeaponIdbyIndex(j));
                if (magicItem != null && magicItem.IsValid())
                    NextDressNum++;
            }
        }

        DressInfo.text = string.Format("({0}/{1})", DressNum, DressDescs.Length);
        for (int i = 1; i < DressDescs.Length; i++)
        {
            string attrStr1 = "";
            string attrStr2 = "";
            string skillDesc = "";

            if(magicDress.GetSuitPropIdbyIndex(2 * i) != -1)
                attrStr1 = PropID.GetAttrValue((PropID.PropertyID)magicDress.GetSuitPropIdbyIndex(2 * i), magicDress.GetSuitSubPropIdbyIndex(2 * i), magicDress.GetSuitSubPropValbyIndex(2 * i));
            if (magicDress.GetSuitPropIdbyIndex(2 * i+1) != -1)
                attrStr2 = PropID.GetAttrValue((PropID.PropertyID)magicDress.GetSuitPropIdbyIndex(2 * i + 1), magicDress.GetSuitSubPropIdbyIndex(2 * i + 1), magicDress.GetSuitSubPropValbyIndex(2 * i + 1));
            
            int SkillID = (i < magicDress.getSkillIDCount() ? magicDress.GetSkillIDbyIndex(i) : -1);
            Tab_SkillEx skillex = TableManager.GetSkillExByID(SkillID, 0);
            if (skillex != null && string.IsNullOrEmpty(skillex.SkillDesc) == false)
                skillDesc = skillex.SkillDesc;

            if (string.IsNullOrEmpty(attrStr1) && string.IsNullOrEmpty(attrStr2) && (skillex == null || string.IsNullOrEmpty(skillex.SkillDesc)))
                continue;

            DressDescs[i].gameObject.SetActive(true);
            string desc = DressNumStr(i) + string.Format(":{0}", StrDictionary.GetClientDictionaryString("#{2829}", tab_CombatMagicWeaponSuit.GetCombatValuebyIndex(i)));
            if(string.IsNullOrEmpty(attrStr1) == false)
            {
                desc += ("\n" + attrStr1);
            }
            if (string.IsNullOrEmpty(attrStr2) == false)
            {
                desc += ("\n" + attrStr2);
            }
            if (string.IsNullOrEmpty(skillDesc) == false)
            {
                desc += ("\n" + skillDesc);
            }
            DressDescs[i].text = desc;
            if (DressNum+ NextDressNum > 0 && i < NextDressNum+DressNum && NextDressNum <= i)
            {
                DressDescs[i].color = Color.white;
            }
            else
            {
                DressDescs[i].color = new Color(0.6f, 0.6f, 0.6f, 1);
            }
        }
    }

    private string DressNumStr(int i)
    {
        switch(i)
        {
            case 1:return StrDictionary.GetClientDictionaryString("#{40312}");
            case 2:return StrDictionary.GetClientDictionaryString("#{40313}");
            case 3:return StrDictionary.GetClientDictionaryString("#{40314}");
            case 4:return StrDictionary.GetClientDictionaryString("#{40315}");
        }
        return "";
    }

    public void SetScollRect()
    {
        if (_ScollContentTran.sizeDelta.y > _ScollHeight)
        {
            _ScollRect.enabled = true;
            _ScollLayout.minHeight = _ScollHeight;

            if (_ScollContentTran.anchoredPosition.y >= _ScollContentTran.sizeDelta.y - _ScollHeight)
            {
                _ScollTips.SetActive(false);
            }
            else
            {
                _ScollTips.SetActive(true);
            }
        }
        else
        {
            _ScollRect.enabled = false;
            _ScollLayout.minHeight = _ScollContentTran.sizeDelta.y;
            _ScollTips.SetActive(false);
        }
    }
}