using System.Collections;
using System.Collections.Generic;
using GCGame;
using GCGame.Table;
using UnityEngine;
using UnityEngine.UI;

public class EnsureMarryPanelCtr : MonoBehaviour
{
    public static EnsureMarryPanelCtr Instance;

    private readonly int _TotalTimes = 60;

    private float countTime;
    public Text desc;
    public ViewFashionItem fashionItem;
    public Text remainTime;
    public RingItem ringItem;
    public List<Image> roleHeadIconList;
    public Transform titleIconParent;
    private int totalTimes;

    private void Awake()
    {
        Instance = this;
    }

    private void OnDestroy()
    {
        Instance = null;
    }

    private void OnEnable()
    {
        totalTimes = _TotalTimes;
        //SetRoleIconList();
        SetRemainTime(totalTimes);
        InitRingItem();
    }

    //预留戒指接口
    public void InitRingItem()
    {
        ringItem.InitRingItem((int) MarryRingCtr.RingType.Marry,
            GameManager.gameManager.PlayerDataPool.MyRingInfoData.GetRingInfo().ringLevel);
    }

    public void InitWeedingType(int weedingId)
    {
        SetDesc(weedingId);
    }

    public void InitFashionItem(int fashionItemId)
    {
        fashionItem.InitItem(fashionItemId, GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Profession);
    }

    public void SetRoleIconList()
    {
        for (var index = 0; index < roleHeadIconList.Count; index++)
            if (GameManager.gameManager.PlayerDataPool.TeamInfo.GetTeamMember(0).IsValid())
                LoadAssetBundle.Instance.SetImageSprite(roleHeadIconList[index],
                    Utils.GetProfessionSpriteName(GameManager.gameManager.PlayerDataPool.TeamInfo.GetTeamMember(index)
                        .Profession));
    }

    public void SetDesc(int weedingId)
    {
        var weedingConfig = TableManager.GetWeedingConfigByID(weedingId, 0);
        if (weedingConfig == null) return;

        InitFashionItem(weedingConfig.GainFashionId);
        SetCanGainTitleIcon(weedingConfig.BundlePathId);
        var name = "";
        if (GameManager.gameManager.PlayerDataPool.TeamInfo.GetTeamMember(0).IsValid())
            name = GameManager.gameManager.PlayerDataPool.TeamInfo.GetTeamMember(0).MemberName;

        desc.text = StrDictionary.GetClientDictionaryString("#{46356}", name, weedingConfig.WeedingName);
    }

    public void SetCanGainTitleIcon(int bundlePathId)
    {
        var bundlePath = TableManager.GetBundlePathByID(bundlePathId, 0);
        if (bundlePath == null) return;

        LoadAssetBundle.Instance.LoadGameObject(LoadAssetBundle.BUNDLE_PATH_EFFECT, bundlePath.Name,
            delegate(string assetName, GameObject resObj, Hashtable hashParam)
            {
                if (resObj != null)
                {
                    var iconObj = Instantiate(resObj);

                    iconObj.transform.SetParent(titleIconParent);
                    iconObj.transform.localPosition = Vector3.zero;
                    iconObj.transform.localRotation = Quaternion.Euler(Vector3.zero);
                    iconObj.transform.localScale = Vector3.one * bundlePath.Scale;
                }
            }, new Hashtable());
    }

    private void Update()
    {
        if (totalTimes > 0)
        {
            countTime += Time.deltaTime;
            if (countTime >= 1.0f)
            {
                countTime -= 1.0f;
                SetRemainTime(--totalTimes);
            }
        }
        else
        {
            OnCloseBtnClick();
        }
    }

    public void SetRemainTime(int time)
    {
        remainTime.text = StrDictionary.GetClientDictionaryString("#{46357}", time);
    }

    public void OnAgreeBtnClick()
    {
        var req = new ReqMarryRet();
        req.retResult = 1;
        req.SendMsg();

        OnCloseBtnClick();
    }

    public void OnRefuseBtnClick()
    {
        var req = new ReqMarryRet();
        req.retResult = 0;
        req.SendMsg();

        OnCloseBtnClick();
    }

    public void OnCloseBtnClick()
    {
        UIManager.CloseUI(UIInfo.EnsureMarryPanel);
    }
}