using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using GCGame.Table;
using GCGame;
using Games.Fellow;
using System.Collections.Generic;
using Module.Log;
/// <summary>
/// 其他玩家宠物面板
/// </summary>
public class OtherPlayerPetPanelCtr : MonoBehaviour {

    #region PetAttr 宠物属性
    public Text petName; //宠物名称
    public UIImgText totlePower; //总战力

    //基础属性
    public Text Summ; //总评语
    public Text grow; //成长
    public Text attack;//攻击
    public Text blood; //气血
    public Text physical; //物理防御
    public Text cultivation; //修为
    public Text magic;   //法术防御
    public Text sawy; //悟性
    //public Text level;  //携带等级
    public Text exp;                // 当前经验/最大经验
    public Slider expSlider;        // 经验进度条
    public Text life;               // 当前生命/最大生命
    public Slider lifeSlider;       // 生命进度条

    //详细属性
    public Text Boneatitude; //根骨资质
    public Text Forceatitude; //精力资质
    public Text Poweratitude; //力量资质
    public Text Smartatitude; //智力资质
    public Text Agileatitude; //敏捷资质
    public Text bone; //根骨
    public Text force; //精力
    public Text power; //力量
    public Text smart; //智力
    public Text agile; //敏捷

    //public Text phyHit;//物理命中
    //public Text magicHit;//法术命中
    //public Text phyAvoid;//物理躲避
    //public Text magicAvoid;//法术躲避 

    public Text detailattack;//攻击
    public Text detailblood; //气血
    public Text detailphysical; //物理防御
    public Text detailmagic;   //法术防御

    public Text hit;  //命中
    public Text avoid; //躲避

    //public Image qualityIcon;  //品质

    public Image GodPetSign;//神兽

    public InitAptitudeStart simpleAptitudeStart;               // 基础信息资质星星
    public InitAptitudeStart detailAptitudeStart;               // 详细信息资质星星

    //技能
    public GameObject[] skillItemObjs = new GameObject[8];

    //模型
    public UICameraTexture modelTexture;

    public GameObject petInfoObj;
    public GameObject noDataObj;
    #endregion

    public TogglesControl toggleControl;
    public List<GameObject> infoPanels;
    public UIContainerSelect petsContainer;         // 玩家宠物列表

    public static OtherPlayerPetPanelCtr Instance;
    void Awake()
    {
        Instance = this;
        toggleControl.OnTogglesSelect += OnMenuClick;
    }

    private void OnDestroy()
    {
        Instance = null;
    }

    private int curPetIndex = 0;  //从第零位开始
    Games.Fellow.Fellow curPetInfo = null;
    private int petCount = 0;
    private enum Arrow
    {
        Left = 1,
        Right = 2,
    }

    // 入口
    // 被返回的协议触发调用
    public void SetPage(int page)
    {
        InitPetsContainer();
        // 等初始化信息结束后再触发显示页面
        toggleControl.SetToggleOn(page);
    }

    // 控制基本信息和详细信息查看界面
    private void OnMenuClick(int page)
    {
        if(page == 0)
        {
            infoPanels[0].SetActive(true);
            infoPanels[1].SetActive(false);
        }
        else if (page == 0)
        {
            infoPanels[0].SetActive(false);
            infoPanels[1].SetActive(true);
        }
        else
        {
            infoPanels[0].SetActive(false);
            infoPanels[1].SetActive(true);
        }
    }

    // 初始化宠物列表
    public void InitPetsContainer()
    {
        FellowContainer petsList = GameManager.gameManager.OtherPlayerData.FellowContainer;
        // 数量不足一个,显示无宠物界面
        if(petsList.GetFellowCount() <= 0)
        {
            noDataObj.gameObject.SetActive(true);
            petInfoObj.gameObject.SetActive(false);
        }
        else
        {
            noDataObj.gameObject.SetActive(false);
            petInfoObj.gameObject.SetActive(true);

            // init Container
            List<Fellow> petsOwn = new List<Fellow>();          // 所有拥有的宠物数组
            List<Fellow> petsSelect = new List<Fellow>();       // 选中数组,默认用第一个
            for(int i = 0; i < petsList.GetFellowCount(); ++i)
            {
                petsOwn.Add(petsList.GetFellowByIndex(i));
            }

            petsSelect.Add(petsList.GetFellowByIndex(0));
            petsContainer.InitSelectContent(petsOwn, petsSelect, OnPetSelect);
        }
    }

    // 宠物列表点击回调
    public void OnPetSelect(object pet)
    {
        curPetInfo = pet as Fellow;
        if(curPetInfo != null)
        {
            // 宠物模型
            ShowModel();
            // 宠物属性
            InitAttr();
            // 宠物技能
            CleanSkillInfo();
            SetSkill();
        }
    }

    // 显示宠物模型
    private void ShowModel()
    {
        var modelTab = curPetInfo.CharModel();
        if (modelTab != null)
        {
            modelTexture.gameObject.SetActive(true);
            modelTexture.InitModelPath(modelTab.ResPath, modelTab, LoadAssetBundle.BUNDLE_PATH_PET, true);
        }
        //新增 判断是神兽吗?***************************************************************
        curPetInfo.SetGodPetSign(GodPetSign);
    }
 
    // 显示宠物属性
    public void InitAttr()
    {
        petName.text = string.Format("Lv.{0}", curPetInfo.Level) + " " + curPetInfo.Name;
        totlePower.text = curPetInfo.CombatValue.ToString();
        Summ.text = curPetInfo.Summ;

        grow.text = ((float)curPetInfo.grow / 10000).ToString();
        attack.text = curPetInfo.attack.ToString();
        blood.text = curPetInfo.blood.ToString();
        physical.text = curPetInfo.physical.ToString();
        cultivation.text = curPetInfo.cultivation.ToString();
        magic.text = curPetInfo.magic.ToString();
        sawy.text = (curPetInfo.sawy * 1.0f / 100.0f).ToString();
        //level.text = curPetInfo.Level.ToString();
        
        Tab_PromoteCultivation atitudeData = TableManager.GetPromoteCultivationByID(curPetInfo.cultivation, 0);
        if (atitudeData == null)
            return;
        float addRate = atitudeData.AddAptitudePer * 1.0f / 100.0f;
        int sawyID = curPetInfo.sawy / 10;

        //新增
        Tab_PromoteSavvy Savvy = TableManager.GetPromoteSavvyByID(sawyID, 0);
        if (Savvy == null)
            return;
        float addRate1 = Savvy.AddAptitude * 1.0f / 100.0f;

        Boneatitude.text = string.Format("{0}<color=#721fadff>(+{1}%)</color>", curPetInfo.Boneatitude, addRate1+addRate);
        Forceatitude.text = string.Format("{0}<color=#721fadff>(+{1}%)</color>", curPetInfo.Forceatitude, addRate1 + addRate);
        Poweratitude.text = string.Format("{0}<color=#721fadff>(+{1}%)</color>", curPetInfo.Poweratitude, addRate1 + addRate);
        Smartatitude.text = string.Format("{0}<color=#721fadff>(+{1}%)</color>", curPetInfo.Smartatitude, addRate1 + addRate);
        Agileatitude.text = string.Format("{0}<color=#721fadff>(+{1}%)</color>", curPetInfo.Agileatitude, addRate1 + addRate);

        bone.text = curPetInfo.bone.ToString();
        force.text = curPetInfo.force.ToString();
        power.text = curPetInfo.power.ToString();
        smart.text = curPetInfo.smart.ToString();
        agile.text = curPetInfo.agile.ToString();
        //新增  **************************************************************
        detailattack.text = attack.text;// curPetInfo.newattack.ToString();
        detailblood.text = blood.text;//curPetInfo.newblood.ToString();
        detailphysical.text = physical.text;//curPetInfo.newphysical.ToString();
        detailmagic.text = magic.text;//curPetInfo.newmagic.ToString();


        //命中
        hit.text = curPetInfo.hit.ToString();
        //phyHit.text = petInfo.hit.ToString();
        //magicHit.text = petInfo.hit.ToString();
        //防御
        physical.text = curPetInfo.physical.ToString();
        magic.text = curPetInfo.magic.ToString();
        //躲避
        avoid.text = curPetInfo.avoid.ToString(); ;
        //phyAvoid.text = petInfo.avoid.ToString();
        //magicAvoid.text = petInfo.avoid.ToString();

        curPetInfo.CalculateAptitudeTotle();

        // 资质星星设置
        if (simpleAptitudeStart != null)
        {
            simpleAptitudeStart.InitStart(curPetInfo.AptitudeTotle);
        }
        detailAptitudeStart.InitStart(curPetInfo.AptitudeTotle);
        if (detailAptitudeStart != null)
        {
            detailAptitudeStart.InitStart(curPetInfo.AptitudeTotle);
        }


        ////宠物品质
        //if (GodPetSign != null)
        //{
        //    //LoadAssetBundle.Instance.SetImageSprite(qualityIcon, Utils.GetFellowQuilityIcon(curPetInfo.Quality));
        //}

        //LoadAssetBundle.Instance.SetImageSprite(GodPetSign, Fellow.IsGodPet());

       


        // 经验条、生命条
        Tab_LevelUp levelup = TableManager.GetLevelUpByID(curPetInfo.Level, 0);
        float expSliderValue = (float)curPetInfo.exp / float.Parse(levelup.FellowExpNeed);
        // 没满级时
        if (levelup != null)
        {
            exp.text = string.Format("{0}/{1}", curPetInfo.exp, levelup.FellowExpNeed);
            expSlider.value = expSliderValue;
            //long maxExp;
            //if(long.TryParse(levelup.FellowExpNeed, out maxExp))
            //{
                // 大坑 为什么这里无法进行除法运算
            //}
            //else
            //{
            //    LogModule.ErrorLog("OtherPlayerPetPanelCtr:Can't convert needExp to float, please check !");
            //}
        }
        // 满级后,最大经验为当前经验
        else
        {
            exp.text = string.Format("{0}/{1}", curPetInfo.exp, curPetInfo.exp);
            expSlider.value =  1.0f;
        }

        Tab_FellowBase fellowBase = TableManager.GetFellowBaseByID(curPetInfo.DataId, 0);
        if (fellowBase != null)
        {
            life.text = string.Format("{0}/{1}", curPetInfo.lift, fellowBase.MaxLife);
            lifeSlider.value = (float)curPetInfo.lift / (float)fellowBase.MaxLife;
        }
        
    }
   
    // 显示宠物技能
    public void SetSkill()
    {
        for (int i = 0; i < skillItemObjs.Length; i++)
        {
            if (i == 0)
            {
                AddStudySkill(curPetInfo, curPetInfo.GY_skillid, i);
                continue;
            }
            if (i == 1)
            {
                AddStudySkill(curPetInfo, curPetInfo.MW_skillid, i);
                continue;
            }
            //AddStudySkill(curPetInfo, curPetInfo.GetOwnSkillId(i - 2), i);
        }
    }

    private void CleanSkillInfo()
    {
        for (int skillIndex = 0; skillIndex < skillItemObjs.Length; skillIndex++)
        {
            GameObject skillHas = skillItemObjs[skillIndex];
            if (skillHas == null)
                return;
            Transform child = skillHas.transform.Find("Child");
            if (child != null)
                child.gameObject.SetActive(false);
        }
    }

    public void AddStudySkill(Fellow fellow, int id, int index)
    {
        if (index > skillItemObjs.Length)
            return;
        GameObject skillHas = skillItemObjs[index];
        if (skillHas == null)
            return;
        Transform child = skillHas.transform.Find("Child");
        Tab_FellowSkill skillInfo = TableManager.GetFellowSkillByID(id, 0);
        if (skillInfo == null)
        {
            if (child != null)
                child.gameObject.SetActive(false);
            return;
        }
        if (fellow.IsHaveSkillId(id) == false)
        {
            if (child != null)
                child.gameObject.SetActive(false);
            return;
        }
        child.gameObject.SetActive(true);
        Transform skillIcon = skillHas.transform.Find("Child/Icon");
        Transform level = skillHas.transform.Find("Child/Level");
        Button Btn = skillHas.GetComponentInChildren<Button>();
        if (skillIcon != null)
        {
            Image icon = skillIcon.gameObject.GetComponent<Image>();
            if (icon != null)
            {
                LoadAssetBundle.Instance.SetImageSprite(icon, skillInfo.SkillIcon);
            }
        }
        if (level != null)
        {
            Text levelText = level.gameObject.GetComponent<Text>();
            if (levelText != null)
            {
                levelText.text = skillInfo.Level.ToString();
            }
        }
        if (Btn != null)
        {
            Btn.onClick.RemoveAllListeners();
            Btn.onClick.AddListener(delegate ()
            {
                OnClickSkillIcon(id, Btn.transform);
            });
        }

    }

    // 点击宠物技能回调,显示技能详情
    public void OnClickSkillIcon(int skillID, Transform pos)
    {
        Tab_FellowSkill skillInfo = TableManager.GetFellowSkillByID(skillID, 0);
        if (skillInfo == null)
            return;
        if (pos != null)
            FellowSkillTooltip.ShowSkillToolTips(skillID, pos.position);
    }
}