using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using GCGame.Table;
using System;

public class SkillRootItem : UIItemSelect, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    public class SkillShowInfo
    {
        public int SkillIndex;
        public Tab_SkillEx SkillExTab;                  // 当前技能属性数据
        public Tab_SkillBase SkillBase;
        public Tab_SkillLearn SkillLearn;               // 主要用于控制显示技能位置,也包含技能类别,开始学习等级
        public Tab_SkillLevelUp SkillLevelUp;           // 下一级的技能数据,包括Id、它的升级技能、消耗/奖励信息

        // 检查是否需要前置技能-在学了前置技能才可学习后面的技能
        public bool IsNeedSkillBase(int skillBaseID)
        {
            var skillFirst = TableManager.GetSkillLevelUpByID(SkillLearn.SkillId, 0);
            for (int i = 0; i < SkillLevelUp.getNeedSkillIdCount(); ++i)
            {
                int needSkillID = skillFirst.GetNeedSkillIdbyIndex(i);
                if (needSkillID > 0)
                {
                    Tab_SkillEx needSkillEx = TableManager.GetSkillExByID(needSkillID, 0);
                    if (needSkillEx == null)
                        return false;

                    if (needSkillEx.BaseId != SkillBase.Id && needSkillEx.BaseId == skillBaseID)
                        return true;
                }
            }
            return false;
        }

        public Tab_SkillEx GetNeedSkillLvUp()
        {
            for (int i = 0; i < SkillLevelUp.getNeedSkillIdCount(); ++i)
            {
                int needSkillID = SkillLevelUp.GetNeedSkillIdbyIndex(i);
                if (needSkillID > 0)
                {
                    Tab_SkillEx needSkillEx = TableManager.GetSkillExByID(needSkillID, 0);
                    if (needSkillEx == null)
                        return null;

                    if (needSkillEx.BaseId != SkillBase.Id)
                    {
                        return needSkillEx;
                    }

                }
            }

            return null;
        }

        public bool IsNeedSkillLearned()
        {
            var needSkill = GetNeedSkillLvUp();
            if (needSkill == null)
            {
                return true;
            }
            else
            {
                bool alreadyLearnNeedSkill = false;
                foreach (var skillInfo in Singleton<ObjManager>.GetInstance().MainPlayer.OwnSkillInfo)
                {
                    var skillEx = skillInfo.SkillExTable;
                    if (skillEx == null)
                        continue;

                    if (skillEx.BaseId == needSkill.BaseId && skillEx.Level >= needSkill.Level)
                    {
                        alreadyLearnNeedSkill = true;
                    }
                }

                return alreadyLearnNeedSkill;
            }
        }
    }

    public Image _Icon;
    public Text _LevelTx;
    public GameObject _LevelGO;
    public Text _LearnTx1;
    public Text _LearnTx2;
    public GameObject _LearnGO1;
    public GameObject _LearnGO2;
    public GameObject _DisableGO;
    public GameObject _PassiveGO;
    public GameObject _NeedItemGO;
    public RectTransform _RectTransform;
    public GameObject _EquipedTag;

    public SkillShowInfo ShowSkillInfo;

    public virtual void ShowSkillItem(SkillShowInfo skillInfo, bool isOtherPlayer)
    {
        UnSelected();
        _InitInfo = skillInfo;
        ShowSkillInfo = skillInfo;
        LoadAssetBundle.Instance.SetImageSprite(_Icon, skillInfo.SkillBase.Icon);
        if (ShowSkillInfo.SkillLearn.ShowPosY > 0)
        {
            _LearnGO1.SetActive(true);
            _LearnGO2.SetActive(false);
            _LearnTx1.text = ShowSkillInfo.SkillLearn.LearnLv.ToString();
        }
        else
        {
            _LearnGO1.SetActive(false);
            _LearnGO2.SetActive(true);
            _LearnTx2.text = ShowSkillInfo.SkillLearn.LearnLv.ToString();
        }

        if (skillInfo.SkillExTab == null)
        {
            _DisableGO.SetActive(true);
            // 尚未学习
            _LevelTx.text = StrDictionary.GetClientDictionaryString("#{6738}");
            if (skillInfo.SkillLevelUp != null)
            {
                if (skillInfo.SkillLevelUp.Level > GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Level)
                {
                    _NeedItemGO.SetActive(false);
                }
                else
                {
                    _NeedItemGO.SetActive(true);
                }
            }
        }
        else
        {
            _DisableGO.SetActive(false);
            _NeedItemGO.SetActive(false);
            Games.SkillModle.OwnSkillData ownSkill = null;
            if (!isOtherPlayer)
            {
                ownSkill = GameManager.gameManager.PlayerDataPool.GetOwnSkillInfo(skillInfo.SkillBase.Id);
            }

            if (ownSkill == null || ownSkill.exSkillLevel == 0)
            {
                _LevelTx.text = StrDictionary.GetClientDictionaryString("#{6739}", skillInfo.SkillExTab.Level.ToString());
            }
            else
            {
                _LevelTx.text = StrDictionary.GetClientDictionaryString("#{6739}", skillInfo.SkillExTab.Level.ToString() + StrDictionary.GetClientDictionaryString("#{4743}", ownSkill.exSkillLevel));
            }
        }

        UpdateRedTips();

        RefreshEquipedTag();
    }

    public void RefreshEquipedTag()
    {
        int skillIdx = -1;
        for (int i = 0; i < Singleton<ObjManager>.GetInstance().MainPlayer.OwnSkillInfo.Length; ++i)
        {
            if (ShowSkillInfo.SkillExTab == null)
                break;

            if (ShowSkillInfo.SkillExTab.SkillExID == Singleton<ObjManager>.GetInstance().MainPlayer.OwnSkillInfo[i].SkillId)
            {
                skillIdx = i;

                break;
            }
        }

        for (int i = 0; i < SkillRootLogic.Instance()._CurSkillPage.SkillIdxs.Length; ++i)
        {
            if (skillIdx != -1 && SkillRootLogic.Instance()._CurSkillPage.SkillIdxs[i] == skillIdx)
            {
                _EquipedTag.SetActive(true);
                return;
            }
        }
        _EquipedTag.SetActive(false);
    }

    #region drag


    public virtual void OnBeginDrag(PointerEventData eventData)
    {
        int state = GameManager.gameManager.PlayerDataPool.GetPropInt(PropID.PropertyID.STATE);
        if ( (state & 2) > 0)
        {
            SkillRootLogic.canDrag = false;
            if(SkillRootLogic.Instance().CanDragSkillItem())
            {
                GUIData.AddNotifyData("#{4748}");
            }
        }
        else
        {
            SkillRootLogic.canDrag = true;
        }

        if(SkillRootLogic.canDrag == false)
        {
            return;
        }

        if (FactionSkillChangeRootCtr.Instance)
        {
            if (_DisableGO.activeSelf)
                return;

            FactionSkillChangeRootCtr.Instance.SetDragSkillIdx(ShowSkillInfo);
            if (!GlobalData.CanDragSkillBySkillIndex(FactionSkillChangeRootCtr.Instance._DragSkillOwnIdx))
            {
                FactionSkillChangeRootCtr.Instance.SetDragSkillIdx(null);
                return;
            }

            FactionSkillChangeRootCtr.Instance._DragIcon.sprite = _Icon.sprite;
            FactionSkillChangeRootCtr.Instance._DragIcon.gameObject.SetActive(true);
        }else if (SkillRootLogic.Instance())
        {
            if (_DisableGO.activeSelf)
                return;
            if (!SkillRootLogic.Instance().CanDragSkillItem())
                return;
            //绝技不能被拖动
            SkillRootLogic.Instance().SetDragSkillIdx(ShowSkillInfo);
            if (!GlobalData.CanDragSkillBySkillIndex(SkillRootLogic.Instance()._DragSkillOwnIdx))
            {
                SkillRootLogic.Instance().SetDragSkillIdx(null);
                return;
            }
            SkillRootLogic.Instance()._DragIcon.sprite = _Icon.sprite;
            SkillRootLogic.Instance()._DragIcon.gameObject.SetActive(true);
        }
    }

    public virtual void OnDrag(PointerEventData eventData)
    {
        if (SkillRootLogic.canDrag == false)
        {
            return;
        }

        if (FactionSkillChangeRootCtr.Instance)
        {
            if (FactionSkillChangeRootCtr.Instance._DragIcon.isActiveAndEnabled)
            {
                SetDraggedPosition(eventData);
            }
        }

        if(SkillRootLogic.Instance())
        {
            if(SkillRootLogic.Instance()._DragIcon.isActiveAndEnabled)
            {
                SetDraggedPosition(eventData);
            }
        }
    }

    protected virtual void SetDraggedPosition(PointerEventData eventData)
    {
        if (SkillRootLogic.Instance())
        {
            var rt = SkillRootLogic.Instance()._DragIcon.rectTransform;
            Vector3 globalMousePos;
            if (RectTransformUtility.ScreenPointToWorldPointInRectangle(SkillRootLogic.Instance().GetComponent<RectTransform>(), eventData.position, eventData.pressEventCamera, out globalMousePos))
            {
                rt.position = globalMousePos;
            }
        }

        if (FactionSkillChangeRootCtr.Instance)
        {
            var rt = FactionSkillChangeRootCtr.Instance._DragIcon.rectTransform;
            Vector3 globalMousePos;
            if (RectTransformUtility.ScreenPointToWorldPointInRectangle(FactionSkillChangeRootCtr.Instance.GetComponent<RectTransform>(), eventData.position, eventData.pressEventCamera, out globalMousePos))
            {
                rt.position = globalMousePos;
            }
        }
    }

    public virtual void OnEndDrag(PointerEventData eventData)
    {
        if (SkillRootLogic.canDrag == false)
        {
            return;
        }

        if (SkillRootLogic.Instance())
        {
            SkillRootLogic.Instance().SetDragSkillIdx(null);
            SkillRootLogic.Instance()._DragIcon.sprite = null;
            SkillRootLogic.Instance()._DragIcon.gameObject.SetActive(false);
        }

       if(FactionSkillChangeRootCtr.Instance)
        {
            FactionSkillChangeRootCtr.Instance.SetDragSkillIdx(null);
            FactionSkillChangeRootCtr.Instance._DragIcon.sprite = null;
            FactionSkillChangeRootCtr.Instance._DragIcon.gameObject.SetActive(false);
        }
    }

    #endregion

    #region 

    public GameObject _RedTips;

    // 更新红点
    public virtual void UpdateRedTips()
    {
        // 增加对他人信息界面支持
        if (SkillRootLogic.Instance() != null 
            && SkillRootLogic.Instance().gameObject.activeInHierarchy 
            && SkillRootLogic.CanSkillItemLvUp(ShowSkillInfo))
        {
            _RedTips.SetActive(true);
        }
        else
        {
            _RedTips.SetActive(false);
        }
    }

    #endregion
}