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 GuildWarRightTop : MonoBehaviour {

    public GameObject Wnd;

    public Text guild1Blood;
    public Text guild2Blood;

    public Image guild1Child;
    public Image guild2Child;

    public Text timeDown;

    private long blood1 = 0;
    private long blood2 = 0;
    // Use this for initialization
    void Awake () {

        Hashtable add = new Hashtable();
        add["name"] = "GuildMatchTowerBlood";
        Games.Events.MessageEventCallBack call = UpdateTowerBlood;
        add["callFun"] = call;
        Games.Events.EventDispatcher.Instance.AddMessageEvent(Games.Events.EventId.GuildWarTowerBlood, add);

        Games.Events.EventDispatcher.Instance.Add(Games.Events.EventId.FunctionChangeBaseBtnActive, HideUI);

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

    void Start()
    {
        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();
        InvokeRepeating("RemainTimeDown", 1, 1);
    }

    void OnDestroy()
    {
        Games.Events.EventDispatcher.Instance.RemoveMessage(Games.Events.EventId.GuildWarFirstSceneInfo, "CapRightTop");
        Games.Events.EventDispatcher.Instance.RemoveMessage(Games.Events.EventId.GuildWarTowerBlood, "GuildMatchTowerBlood");
        Games.Events.EventDispatcher.Instance.RemoveMessage(Games.Events.EventId.GuildWarTowerBlood, "HideUI");
        CancelInvoke();
    }

    public void HideUI(object obj)
    {
        bool active = (bool)obj;
        Wnd.SetActive(!active);
    }
    long totleNaijiu;
    public void FreshBlood()
    {
        guild1Blood.text = blood1.ToString();
        guild2Blood.text = blood2.ToString();

        guild1Child.fillAmount = blood1 * 1.0f / totleNaijiu;
        guild2Child.fillAmount = blood2 * 1.0f / totleNaijiu;
    }

    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;
        var caps = TableManager.GetServerLvlCap().Values;
        foreach (var cap in caps)
        {
            if (cap.LevelCap == packet.Curlvlcap)
            {
                long.TryParse(cap.GuildWarTower, out totleNaijiu);
                return;
            }
        }
    }

    public void UpdateTowerBlood(Hashtable add,Hashtable send)
    {
        if (send.ContainsKey("data") == false)
            return;
        GC_UNION_MATCH_TOWER_INFO packet = (GC_UNION_MATCH_TOWER_INFO)send["data"];
        if (packet == null)
            return;

        if (packet.towerhpCount>0)
            blood1 = packet.GetTowerhp(0);
        if(packet.towerhpCount>1)
            blood2 = packet.GetTowerhp(1);

        FreshBlood();
    }

    public void RemainTimeDown()
    {
        float leaveTime = SceneData.CopyOverTime - Time.time;
        timeDown.text = Utils.GetTimeStr1((int)leaveTime);
    }

    public void Click_WarInfo()
    {
        UIManager.ShowUI(UIInfo.GuildWarInfo, null, null);
    }

}