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

    public GameObject CloneObj;

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

    void OnDestroy()
    {
        Games.Events.EventDispatcher.Instance.RemoveMessage(Games.Events.EventId.GuildWarInvite, "MemInfoRecv");
    }

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

    public void MemInfoRecv(Hashtable add,Hashtable send)
    {
        if (send.ContainsKey("data") == false)
            return;
        GC_UNION_MATCH_NOT_IN_SCENE packet = (GC_UNION_MATCH_NOT_IN_SCENE)send["data"];
        if (packet == null)
            return;
        for(int i=0;i<packet.infoCount;i++)
        {
            NotInUnionMatchPlayerInfo info = packet.GetInfo(i);
            CloneItem(info.Name,info.Level,info.Guid,info.Prof, (i % 2) == 0);
        }
    }

    void CloneItem(string name,int level,ulong Guid,int prof,bool isEvent)
    {
        if (CloneObj == null)
            return;
        GameObject newObj = GameObject.Instantiate(CloneObj);
        if (newObj == null)
            return;
        newObj.SetActive(true);
        newObj.transform.SetParent(CloneObj.transform.parent);
        newObj.transform.localPosition = CloneObj.transform.localPosition;
        newObj.transform.localScale = CloneObj.transform.localScale;

        Transform back1 = newObj.transform.Find("back1");
        Transform back2 = newObj.transform.Find("back2");
        if(back1!=null)
        {
            back1.gameObject.SetActive(isEvent);
        }
        if (back2 != null)
        {
            back2.gameObject.SetActive(isEvent==false);
        }
        Transform profession = newObj.transform.Find("professionIcon");
        if(profession!=null)
        {
            Image icon = profession.gameObject.GetComponent<Image>();
            if (icon != null)
            {
                LoadAssetBundle.Instance.SetImageSprite(icon, Utils.GetProfessionSpriteName(prof));
            }
        }
        
        Text[] texts = newObj.GetComponentsInChildren<Text>();
        for(int i=0;i<texts.Length;i++)
        {
            if(texts[i].gameObject.name == "name")
            {
                texts[i].text = name;
            }
            if(texts[i].gameObject.name == "level")
            {
                texts[i].text = level.ToString();
            }
        }
        Button btn = newObj.GetComponentInChildren<Button>();
        if(btn!=null)
        {
            btn.onClick.AddListener(delegate ()
            {
                btn.interactable = false;
                CG_INVITE_GUILD_MEMBER send = (CG_INVITE_GUILD_MEMBER)PacketDistributed.CreatePacket(MessageID.PACKET_CG_INVITE_GUILD_MEMBER);
                send.SetGuid(Guid);
                send.SendPacket();
            });
        }
    }

}