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;

public class GuildWarInfo : MonoBehaviour {

    public UITagPanel _TagPanel;

    public GameObject georgeBtn;//玄霄怒按钮
    public GameObject generalBtn;//将军威按钮

    //主战场信息
    public Text main_generalName;  //将军威帮会名称
    public Text main_georgeName;   //玄霄怒帮会名称 
    public Text main_generalNaijiu;//将军威耐久
    public Text main_georgeNaijiu; //玄霄怒耐久
    public Text main_genpersons;   //将军威主战场人数
    public Text main_geopersons;   //玄霄怒主战场人数
    public Text main_genKills;     //将军威主战场灭敌数
    public Text main_geoKills;     //玄霄怒主战场灭敌数
    public Text main_genkillMax;   //将军威主战场杀敌最多
    public Text main_geokillMax;   //玄霄怒主战场杀敌最多
    public Text main_gentower;     //将军威箭塔数
    public Text main_geotower;     //玄霄怒箭塔数

    //副战场信息
    public Text m_generalName;  //将军威帮会名称
    public Text m_georgeName;   //玄霄怒帮会名称 
    public Text m_generalNaijiu;//将军威耐久
    public Text m_georgeNaijiu; //玄霄怒耐久
    public Text m_genpersons;   //将军威副战场人数
    public Text m_geopersons;   //玄霄怒副战场人数
    public Text m_genKills;     //将军威副战场杀敌最多
    public Text m_geoKills;     //玄霄怒副战场杀敌最多
    public Text m_genkillMax;   //将军威副战场灭敌数
    public Text m_geokillMax;   //玄霄怒副战场灭敌数
    public Text m_genkillGodAnim;   //将军威副战场杀死神兽数
    public Text m_geokillGodAnim;   //玄霄怒副战场杀死神兽数
    public Text m_gengiveMax;     //将军威维修最多者
    public Text m_geogiveMax;     //玄霄怒维修最多者
    public Text m_gengiveCount;     //将军威总维修次数
    public Text m_geogiveCount;     //玄霄怒总维修次数


    public GameObject memWargenInfoList; //将军威
    public GameObject memWargeoInfoList; //玄霄怒

    public UIContainerBase m_WarMain1;
    public UIContainerBase m_WarMain2;

    public UIContainerBase m_WarFu1;
    public UIContainerBase m_WarFu2;

    private long generalNaijiu = 0;
    private long georgeNaijiu = 0;
    private long totleNaijiu = -1;

    void Awake()
    {
        Hashtable add = new Hashtable();
        add["name"] = "MemInfoRecv";
        Games.Events.MessageEventCallBack call = MainSceneInfo;
        add["callFun"] = call;
        Games.Events.EventDispatcher.Instance.AddMessageEvent(Games.Events.EventId.GuildWarFirstSceneInfo, add);

        Hashtable add1 = new Hashtable();
        add1["name"] = "MemInfoRecv1";
        Games.Events.MessageEventCallBack call1 = MainSceneInfo1;
        add1["callFun"] = call1;
        Games.Events.EventDispatcher.Instance.AddMessageEvent(Games.Events.EventId.GuildWarSecondSceneInfo, add1);

        Hashtable add2 = new Hashtable();
        add2["name"] = "MemsInfo";
        Games.Events.MessageEventCallBack call2 = MemsInfo;
        add2["callFun"] = call2;
        Games.Events.EventDispatcher.Instance.AddMessageEvent(Games.Events.EventId.GuildWarMemSceneInfo, add2);

        Hashtable add3 = new Hashtable();
        add3["name"] = "Cap";
        Games.Events.MessageEventCallBack call3 = UpdateCap;
        add3["callFun"] = call3;
        Games.Events.EventDispatcher.Instance.AddMessageEvent(Games.Events.EventId.GuildWarFirstSceneInfo, add3);
    }

    void Start()
    {
        if (Singleton<ObjManager>.Instance.MainPlayer != null)
        {
            Singleton<ObjManager>.Instance.MainPlayer.GuildWarUnionInfoReq(CG_GUILD_UNION_MATCH_REQ.UNION_MATCH_REQ.FIRST_SCENE_INFO);
            Singleton<ObjManager>.Instance.MainPlayer.GuildWarUnionInfoReq(CG_GUILD_UNION_MATCH_REQ.UNION_MATCH_REQ.SECOND_SCENE_INFO);
            CG_REQ_SERVER_LVL_CAP packet = (CG_REQ_SERVER_LVL_CAP)PacketDistributed.CreatePacket(MessageID.PACKET_CG_REQ_SERVER_LVL_CAP);
            packet.Noparam = 1;
            packet.SendPacket();
        }
    }

    public void UpdateCap(Hashtable add, Hashtable send)
    {
        if (send.ContainsKey("Cap") == false)
            return;
        GC_RET_SERVER_LVL_CAP packet = (GC_RET_SERVER_LVL_CAP)send["Cap"];
        if (packet == null)
            return;
        foreach(var cap in TableManager.GetServerLvlCap().Values)
        {
            if(cap.LevelCap == packet.Curlvlcap)
            {
                long.TryParse(cap.GuildWarTower, out totleNaijiu);
                main_generalNaijiu.text = string.Format("{0}/{1}", generalNaijiu, totleNaijiu);
                main_georgeNaijiu.text = string.Format("{0}/{1}", georgeNaijiu, totleNaijiu);
                return;
            }
        }
    }

    List<UnionMatchPlayerInfo> main1List = new List<UnionMatchPlayerInfo>();
    List<UnionMatchPlayerInfo> main2List = new List<UnionMatchPlayerInfo>();

    void OnDestroy()
    {
        Games.Events.EventDispatcher.Instance.RemoveMessage(Games.Events.EventId.GuildWarFirstSceneInfo, "MemInfoRecv");
        Games.Events.EventDispatcher.Instance.RemoveMessage(Games.Events.EventId.GuildWarSecondSceneInfo, "MemInfoRecv1");
        Games.Events.EventDispatcher.Instance.RemoveMessage(Games.Events.EventId.GuildWarMemSceneInfo, "MemsInfo");
        Games.Events.EventDispatcher.Instance.RemoveMessage(Games.Events.EventId.GuildWarFirstSceneInfo, "Cap");
    }

    public void MemsInfo(Hashtable add, Hashtable send)
    {
        if (send.ContainsKey("data") == false)
            return;
        GC_UNION_MATCH_FIRST_SCENE_PLAYER_INFO packet = (GC_UNION_MATCH_FIRST_SCENE_PLAYER_INFO)send["data"];
        if (packet.Hasinfo==0)
        {
            main_genkillMax.text = "";
            main_geokillMax.text = "";
        }
        else
        {
            int killMax1 = 0;
            string killerMax1 = StrDictionary.GetClientDictionaryString("#{1006}");
            main1List.Clear();
            for (int i=0;i<packet.player1Count;i++)
            {
                UnionMatchPlayerInfo info = packet.GetPlayer1(i);
                if(info.Kill > killMax1)
                {
                    killMax1 = info.Kill;
                    killerMax1 = info.Name;
                }
                main1List.Add(info);
            }

            int killMax2 = 0;
            string killerMax2 = StrDictionary.GetClientDictionaryString("#{1006}");
            main2List.Clear();
            for (int i = 0; i < packet.player2Count; i++)
            {
                UnionMatchPlayerInfo info = packet.GetPlayer2(i);
                if (info.Kill > killMax2)
                {
                    killMax2 = info.Kill;
                    killerMax2 = info.Name;
                }
                main2List.Add(info);
            }

            if(memWargenInfoList.activeSelf)
                m_WarMain1.InitContentItem(main1List, null, null);
            if(memWargeoInfoList.activeSelf)
                m_WarMain2.InitContentItem(main2List, null, null);
            main_genkillMax.text = killerMax1;
            main_geokillMax.text = killerMax2;
        }
    }

    public void MainSceneInfo(Hashtable add,Hashtable send)
    {
        if (send.ContainsKey("data") == false)
            return;
        GC_UNION_MATCH_FIRST_SCENE_INFO packet = (GC_UNION_MATCH_FIRST_SCENE_INFO)send["data"];
        if (packet == null)
            return;
        if (packet.guildNameCount >= 1)
        {
            main_generalName.text = packet.GetGuildName(0);
            if(GameManager.gameManager.PlayerDataPool.GuildInfo.GuildName == packet.GetGuildName(0))
            {
                georgeBtn.SetActive(true);
                generalBtn.SetActive(false);
                memWargenInfoList.SetActive(true);
                memWargeoInfoList.SetActive(false);
            }
        }
        if (packet.guildNameCount >= 2)
        {
            main_georgeName.text = packet.GetGuildName(1);
            if (GameManager.gameManager.PlayerDataPool.GuildInfo.GuildName == packet.GetGuildName(1))
            {
                georgeBtn.SetActive(false);
                generalBtn.SetActive(true);
                memWargenInfoList.SetActive(false);
                memWargeoInfoList.SetActive(true);
            }
        }
        if(packet.TowerHPCount>=1)
        {
            generalNaijiu = packet.GetTowerHP(0);
            main_generalNaijiu.text = string.Format("{0}/{1}", generalNaijiu, packet.GetTowerHP(2));
        }
        if (packet.TowerHPCount >= 2)
        {
            georgeNaijiu = packet.GetTowerHP(1);
            main_georgeNaijiu.text = string.Format("{0}/{1}", georgeNaijiu, packet.GetTowerHP(2));
        }

        if (packet.PeopleCount >= 1)
            main_genpersons.text = packet.GetPeople(0).ToString();
        if (packet.PeopleCount >= 2)
            main_geopersons.text = packet.GetPeople(1).ToString();
        main_genKills.text = StrDictionary.GetClientDictionaryString("#{1006}");
        main_geoKills.text = StrDictionary.GetClientDictionaryString("#{1006}");
        if (packet.TotalKillCount >= 1)
            main_genKills.text = packet.GetTotalKill(0).ToString();
        if (packet.TotalKillCount >= 2)
            main_geoKills.text = packet.GetTotalKill(1).ToString();

        if (packet.TowerCountCount >= 1)
            main_gentower.text = packet.GetTowerCount(0).ToString();
        if (packet.TowerCountCount >= 2)
            main_geotower.text = packet.GetTowerCount(1).ToString();
    }

    public void MainSceneInfo1(Hashtable add, Hashtable send)
    {
        if (send.ContainsKey("data") == false)
            return;
        GC_UNION_MATCH_SECOND_SCENE_INFO packet = (GC_UNION_MATCH_SECOND_SCENE_INFO)send["data"];
        if (packet == null)
            return;
        if (packet.guildNameCount >= 1)
            m_generalName.text = packet.GetGuildName(0);
        if (packet.guildNameCount >= 2)
            m_georgeName.text = packet.GetGuildName(1);

        if (packet.PeopleCount >= 1)
            m_genpersons.text = packet.GetPeople(0).ToString();
        if (packet.PeopleCount >= 2)
            m_geopersons.text = packet.GetPeople(1).ToString();

        if (packet.TotalKillCount >= 1)
            m_genKills.text = packet.GetTotalKill(0).ToString();
        if (packet.TotalKillCount >= 2)
            m_geoKills.text = packet.GetTotalKill(1).ToString();

        if (packet.KillBossCount >= 1)
            m_genkillGodAnim.text = packet.GetKillBoss(0).ToString();
        if (packet.KillBossCount >= 2)
            m_geokillGodAnim.text = packet.GetKillBoss(1).ToString();

        if (packet.TotalMaintainCount >= 1)
            m_gengiveCount.text = packet.GetTotalMaintain(0).ToString();
        if (packet.TotalMaintainCount >= 2)
            m_geogiveCount.text = packet.GetTotalMaintain(1).ToString();

        List<UnionMatchPlayerInfo> main1List = new List<UnionMatchPlayerInfo>();
        int killer1Max = 0;
        int MaintainMax1 = 0;
        string Maintainer1 = "";
        string killer1 = StrDictionary.GetClientDictionaryString("#{1006}");
        for (int i=0;i<packet.player1Count;i++)
        {
            UnionMatchPlayerInfo info = packet.GetPlayer1(i);
            if (info.Kill > killer1Max)
            {
                killer1Max = info.Kill;
                killer1 = info.Name;
            }
            if (info.Maintain > MaintainMax1)
            {
                MaintainMax1 = info.Maintain;
                Maintainer1 = info.Name;
            }
            main1List.Add(info);
        }

        List<UnionMatchPlayerInfo> main2List = new List<UnionMatchPlayerInfo>();
        int killer2Max = 0;
        int MaintainMax2 = 0;
        string Maintainer2 = "";
        string killer2 = StrDictionary.GetClientDictionaryString("#{1006}");
        for (int i = 0; i < packet.player2Count; i++)
        {
            UnionMatchPlayerInfo info = packet.GetPlayer2(i);
            if (info.Kill > killer2Max)
            {
                killer2Max = info.Kill;
                killer2 = info.Name;
            }
            if (info.Maintain > MaintainMax2)
            {
                MaintainMax2 = info.Maintain;
                Maintainer2 = info.Name;
            }
            main2List.Add(info);
        }

        m_WarFu1.InitContentItem(main1List, null, null);
        m_WarFu2.InitContentItem(main2List, null, null);

        m_genkillMax.text = killer1;
        m_geokillMax.text = killer2;

        m_gengiveMax.text = Maintainer1;
        m_geogiveMax.text = Maintainer2;
}

    public void ChangeMemInfoWnd_Click()
    {
        bool gen = memWargenInfoList.activeSelf ? false : true;
        memWargenInfoList.SetActive(gen);
        memWargeoInfoList.SetActive(!gen);

        if (memWargenInfoList.activeSelf)
            m_WarMain1.InitContentItem(main1List, null, null);
        if (memWargeoInfoList.activeSelf)
            m_WarMain2.InitContentItem(main2List, null, null);

        georgeBtn.SetActive(gen);
        generalBtn.SetActive(!gen);
    }

    public void Close_Click()
    {
        UIManager.CloseUI(UIInfo.GuildWarInfo);
    }
}