using Games.ChatHistory;
using Games.GlobeDefine;
using Games.LogicObj;
using GCGame;
using GCGame.Table;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class Obj_PlayerHeadUI : Obj_HeadUI
{
    public static UIPathData pathData
    {
        get { return UIInfo.OtherPlayerHeadInfo; }
    }

    public override UIPathData uiPathData
    {
        get { return pathData; }
    }

    protected override bool HideByOption
    {
        get { return true; }
    }

    public Slider GreenHpSlider;
    public Slider PurpleHpSlider;
    private Slider HpSlider;
    public Text m_LabelTitleBoard;
    public GameObject _TitleIconQuadPanel;
    public Image m_LabelTitleBoardImage;
    public Text m_LabelPaoShangBoard;
    public Text m_VipText;
    public Image m_SprVip;

    public GameObject m_SprCaptain;
    public GameObject m_SprTeamMem;

    public Image m_SprCanKill;
    public Image m_SprKillMe;
    public GameObject m_ObjChatBubble;
    public GameObject m_ObjIcons;
    public ChatBubbleLogic m_ChatBubbleLogic;

    public RectTransform NameRect;
    public RectTransform IconRect;

    private int m_titleID = -1;
    private string m_title = "";

    public GameObject _WatchSign;
    public void ShowWatchSign(bool isShow)
    {
        if(_WatchSign)
            _WatchSign.SetActive(isShow);
    }

    protected override void OnEnable()
    {
        base.OnEnable();
        if (m_ChatBubbleLogic != null)
            m_ChatBubbleLogic.gameObject.SetActive(false);
        ShowWatchSign(false);
    }

    public void ResetNameBoradPos(bool showServerName)
    {
        if(showServerName)
        {
            NameRect.sizeDelta = new Vector2(320, 30);
            IconRect.localPosition = new Vector3(-60, -21.7f, 0);
        }
        else
        {
            //NameRect.sizeDelta = new Vector2(180, 30);
            IconRect.localPosition = new Vector3(0, -21.7f, 0);
        }
    }

    public void SetNewHp(float rate)
    {
        if(HpSlider != null && HpSlider.gameObject.activeSelf)
            HpSlider.value = rate;
    }

    // 血条显示以及颜色
    public void HpSliderShowLogic()
    {
        GreenHpSlider.gameObject.SetActive(false);
        PurpleHpSlider.gameObject.SetActive(false);

        if (Target == null)
            return;
        Obj_OtherPlayer otherPlayer = Target as Obj_OtherPlayer;
        if (otherPlayer == null)
            return;

        // 如果是主角,用绿色显示血条
        if (Target.ObjType == GameDefine_Globe.OBJ_TYPE.OBJ_MAIN_PLAYER)
        {
            HpSlider = GreenHpSlider;
            HpSlider.gameObject.SetActive(true);
            return;
        }

        // 非主角,分场景显示血条
        Tab_SceneClass scene = TableManager.GetSceneClassByID(GameManager.gameManager.RunningScene, 0);
        if (scene == null)
            return;

        //门派挑战和关宁做一下特殊处理 (还有紫荆之巅)
        if (GameManager.gameManager.RunningScene == (int)GameDefine_Globe.SCENE_DEFINE.SCENE_GUANNING
            || GameManager.gameManager.RunningScene == (int)GameDefine_Globe.SCENE_DEFINE.SCENE_FACIONCHALLENGE
            || GameManager.gameManager.RunningScene == GameManager.gameManager.PlayerDataPool.pvpIfo._PvpBattleSceneId
            || GameManager.gameManager.m_RunningScene == 658)
        {
            bool result = (otherPlayer.BaseAttr.WarForce != GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.WarForce 
                || otherPlayer.BaseAttr.Force != GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Force);
            HpSlider = (result ? PurpleHpSlider : GreenHpSlider);
            if (GameManager.gameManager.m_RunningScene == 658)
            {
                //紫荆之巅中其他玩家显示紫色血条
                HpSlider = (result ? GreenHpSlider : PurpleHpSlider);
            }
            HpSlider.gameObject.SetActive(true);
            return;
        }
   

        if (Reputation.IsEnemy(otherPlayer) && scene.Type == 1)
        {
            HpSlider = PurpleHpSlider;
            HpSlider.gameObject.SetActive(true);
            return;
        }
        bool isTeam = GameManager.gameManager.PlayerDataPool.IsTeamMem(otherPlayer.GUID);
        if (isTeam && (scene.Type == 2 || scene.Type == 1))
        {
            HpSlider = GreenHpSlider;
            HpSlider.gameObject.SetActive(true);
            return;
        }
    }

    public void HideHp(bool state)
    {
        HpSliderShowLogic();
    }

    public void UpdateOtherPkModel(Obj_OtherPlayer obj)
    {
        m_SprCanKill.gameObject.SetActive(false);
        m_SprKillMe.gameObject.SetActive(false);
        if (obj.ObjType == GameDefine_Globe.OBJ_TYPE.OBJ_MAIN_PLAYER)
            return;
        Tab_SceneClass sceneInfo = TableManager.GetSceneClassByID(GameManager.gameManager.RunningScene, 0);
        if (sceneInfo == null)
            return;
        Tab_PVPRule pvpRule = TableManager.GetPVPRuleByID(sceneInfo.PVPRule, 0);
        if (pvpRule == null)
            return;
        if (Singleton<ObjManager>.Instance.MainPlayer != null)
        {
            if (GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Level <= pvpRule.ProtectLevel)
            {
                return;
            }
        }

        if (Reputation.IsCanPk(obj))
        {
            SetNameColor( Utils.GetColorByString(StrDictionary.GetClientDictionaryString("#{5556}")));
        }
        if (Reputation.IsCanBeAttack(obj))
            m_SprKillMe.gameObject.SetActive(true);
        HpSliderShowLogic();
    }

    public void UpdateVipInfo(int nVipCost)
    {
        if (m_SprVip != null)
        {
            if (nVipCost > 0)
            {
                m_SprVip.gameObject.SetActive(true);

                float fShifty = m_SprVip.transform.localPosition.y;

                LoadAssetBundle.Instance.SetImageSprite(m_SprVip, VipData.GetVipImage(nVipCost));
            }
            else
            {
                
            }m_SprVip.gameObject.SetActive(false);
        }
        if (nVipCost > 0)
        {
            //m_VipText.gameObject.SetActive(true);
            //m_VipText.text = "VIP" + nVipCost.ToString();
        }
        else
            m_VipText.gameObject.SetActive(false);
    }

    public void UpdateCaptainIcon(bool state)
    {
        if (state == false)
        {
            m_SprCaptain.SetActive(false);
            m_SprTeamMem.SetActive(false);
            return;
        }

        if (Target == null)
            return;
        Obj_OtherPlayer otherPlayer = Target as Obj_OtherPlayer;
        if (otherPlayer == null)
            return;
        bool isCaptain = GameManager.gameManager.PlayerDataPool.TeamInfo.IsCaptain(otherPlayer.GUID);
        if (otherPlayer.GUID == GlobeVar.INVALID_GUID)
            isCaptain = false;

        bool isMember = GameManager.gameManager.PlayerDataPool.IsTeamMem(otherPlayer.GUID);
        m_SprCaptain.SetActive(isCaptain);
       // var _captainImage = m_SprCaptain.GetComponent<Image>();
       //if(_captainImage)
       // {
       //     LoadAssetBundle.Instance.SetImageSprite(_captainImage,
       //    Utils.GetCaptainLevelIcon(otherPlayer.BaseAttr.CaptainWelfareLevel));
       // }

        m_SprTeamMem.SetActive(!isCaptain && isMember);
    }

    public void ShowTitleInvestitiveByTable(int titleID, string strTitleInvestitive)
    {
        m_titleID = -1;
        m_title = "";

        Tab_TitleData tabTitle = TableManager.GetTitleDataByID(titleID, 0);
        if(tabTitle == null)
        {
            _TitleIconQuadPanel.SetActive(false);
            m_LabelTitleBoard.gameObject.SetActive(false);
            return;
        }
        if(!string.IsNullOrEmpty(tabTitle.NameImage))
        {
            _TitleIconQuadPanel.SetActive(true);
            //m_LabelTitleBoardImage.gameObject.SetActive(true);
            m_LabelTitleBoard.gameObject.SetActive(false);

            LoadAssetBundle.Instance.SetImageSprite(m_LabelTitleBoardImage, tabTitle.NameImage,
               delegate (bool isSucess, GameObject obj)
               {
                   ClearTitleIconEffect();
                   m_LabelTitleBoardImage.SetNativeSize();
                   var effectData = TableManager.GetEffectByID(tabTitle.EffectId, 0);
                   if (effectData != null)
                       LoadAssetBundle.Instance.LoadGameObject(LoadAssetBundle.BUNDLE_PATH_EFFECT, effectData.Path, (string assetName, GameObject resObj, Hashtable hashParam) =>
                       {
                           if (resObj != null)
                           {
                               var effectObj = GameObject.Instantiate(resObj);
                               effectObj.transform.SetParent(m_LabelTitleBoardImage.transform);
                               effectObj.transform.localScale = Vector3.one;
                               effectObj.transform.localPosition = Vector3.zero;
                               effectObj.transform.localRotation = Quaternion.Euler(Vector3.zero);
                           }
                       }, null);
               });

            return;
        }else
        {
            ClearTitleIconEffect();
            m_LabelTitleBoardImage.sprite = null;
        }
        _TitleIconQuadPanel.SetActive(false);
        //m_LabelTitleBoardImage.gameObject.SetActive(false);
        if (strTitleInvestitive==null || strTitleInvestitive.Equals(""))
        {
            m_LabelTitleBoard.gameObject.SetActive(false);
            return;
        }
        if (null != m_LabelTitleBoard)
        {
            if (tabTitle != null)
            {
                string strTitle = "";
                strTitle = string.Format("<color=#{0}>{1}</color>", tabTitle.ColorLevel, strTitleInvestitive);
                m_LabelTitleBoard.gameObject.SetActive(strTitle.Length > 0);
                m_LabelTitleBoard.text = strTitle;
            }
            else
            {
                m_LabelTitleBoard.gameObject.SetActive(strTitleInvestitive.Length > 0);
                m_LabelTitleBoard.text = strTitleInvestitive;
            }
        }

        if (GameManager.gameManager.RunningScene == (int)GameDefine_Globe.SCENE_DEFINE.SCENE_GUANNING)
        {
            m_LabelTitleBoard.text = "";
            m_LabelTitleBoard.gameObject.SetActive(false);
        }
        m_titleID = titleID;
        m_title = strTitleInvestitive;
    }

    void ClearTitleIconEffect()
    {
        for (int index = 0; index < m_LabelTitleBoardImage.transform.childCount; index++)
        {
            Destroy(m_LabelTitleBoardImage.transform.GetChild(index).gameObject);
        }
    }

    public void ShowTitleInvestitive(string strTitleInvestitive)
    {
        int titleId = GameManager.gameManager.PlayerDataPool.TitleInvestitive.GetCurrentTitleID();
        Tab_TitleData tabTitle = TableManager.GetTitleDataByID(titleId, 0);
        if (tabTitle != null && !string.IsNullOrEmpty(tabTitle.NameImage))
        {
            _TitleIconQuadPanel.SetActive(true);
            //m_LabelTitleBoardImage.gameObject.SetActive(true);
            m_LabelTitleBoard.gameObject.SetActive(false);
            LoadAssetBundle.Instance.SetImageSprite(m_LabelTitleBoardImage, tabTitle.NameImage, delegate (bool isSucess, GameObject obj)
            {
                ClearTitleIconEffect();
                m_LabelTitleBoardImage.SetNativeSize();
                var effectData = TableManager.GetEffectByID(tabTitle.EffectId, 0);
                if (effectData != null)
                    LoadAssetBundle.Instance.LoadGameObject(LoadAssetBundle.BUNDLE_PATH_EFFECT, effectData.Path, (string assetName, GameObject resObj, Hashtable hashParam) =>
                    {
                        if (resObj != null)
                        {
                            var effectObj = GameObject.Instantiate(resObj);
                            effectObj.transform.SetParent(m_LabelTitleBoardImage.gameObject.transform);
                            effectObj.transform.localScale = Vector3.one;
                            effectObj.transform.localPosition = Vector3.zero;
                            effectObj.transform.localRotation = Quaternion.Euler(Vector3.zero);
                        }
                    }, null);
            });
            return;
        }
        else
        {
            ClearTitleIconEffect();
            m_LabelTitleBoardImage.sprite = null;
        }

        _TitleIconQuadPanel.SetActive(false);
        //m_LabelTitleBoardImage.gameObject.SetActive(false);
        if (strTitleInvestitive == null || strTitleInvestitive.Equals(""))
        {
            m_LabelTitleBoard.gameObject.SetActive(false);
            return;
        }
        if (tabTitle != null)
        {
            string strTitle = "";
            strTitle = string.Format("<color=#{0}>{1}</color>", tabTitle.ColorLevel, strTitleInvestitive);
            m_LabelTitleBoard.gameObject.SetActive(strTitle.Length > 0);
            m_LabelTitleBoard.text = strTitle;
        }
        else
        {
            m_LabelTitleBoard.gameObject.SetActive(string.IsNullOrEmpty(strTitleInvestitive) == false);
            m_LabelTitleBoard.text = strTitleInvestitive;
        }

        if (GameManager.gameManager.RunningScene == (int)GameDefine_Globe.SCENE_DEFINE.SCENE_GUANNING)
        {
            m_LabelTitleBoard.gameObject.SetActive(false);
            m_LabelTitleBoard.text = "";
        }
    }

    public virtual void UpdatePaoShangIcon(string title)
    {
        m_LabelPaoShangBoard.gameObject.SetActive(title.Length>0);
        m_LabelPaoShangBoard.text = title;

        if (GameManager.gameManager.RunningScene == (int)GameDefine_Globe.SCENE_DEFINE.SCENE_GUANNING)
        {
            m_LabelPaoShangBoard.gameObject.SetActive(false);
            m_LabelPaoShangBoard.text = "";
        }
    }

    public void ShowOriginalHeadInfo(bool bShow)
    {
        if (null != m_ObjIcons)
        {
            m_ObjIcons.SetActive(bShow);
        }
        if(null != HpSlider)
        {
            HpSlider.gameObject.SetActive(bShow);
        }
        if(null != m_LabelPaoShangBoard)
        {
            m_LabelPaoShangBoard.gameObject.SetActive(bShow);
        }
    }

    public override void ShowChatBubble(ChatHistoryItem text)
    {
        if (null != m_ChatBubbleLogic)
        {
            m_ChatBubbleLogic.Show(text);
        }
    }
    public override void HideChatBubble(bool state)
    {
        if (null != m_ChatBubbleLogic)
        {
            m_ChatBubbleLogic.gameObject.SetActive(state);
        }
    }

}