using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using Games.Item;
using System.Collections.Generic;
using GCGame.Table;
using Games.GlobeDefine;


public class CommunityLargeImages : UIControllerBase<CommunityLargeImages>
{
    public NetImage[] _Images;
    public Button _BtnLeft;
    public Button _BtnRight;

    private int _ValidImageCnt = 0;
    private int _ShowingIdx = 0;

    private void Update()
    {
        UpdateLoadingEffect();
    }

    public void ShowLargeImages(NetDoubleImage[] netImgs, int selectedIdx)
    {
        _ValidImageCnt = 0;
        for (int i = 0; i < netImgs.Length; ++i)
        {
            if (!string.IsNullOrEmpty(netImgs[i].LargeImageUrl))
            {
                _Images[i].SetNetImage(netImgs[i].LargeImageUrl, netImgs[i].PlayerGuid, false);
                _Images[i].GetComponent<RectTransform>().anchoredPosition = new Vector2(10000, 10000);
                ++_ValidImageCnt;
            }
        }
        _ShowingIdx = selectedIdx;
        RefreshShow();
    }

    public void RefreshShow()
    {
        for (int i = 0; i < _Images.Length; ++i)
        {
            _Images[i].GetComponent<RectTransform>().anchoredPosition = new Vector2(10000, 10000);
        }
        _Images[_ShowingIdx].GetComponent<RectTransform>().anchoredPosition = new Vector2(0, 0);

        if (_ShowingIdx == 0)
        {
            _BtnLeft.gameObject.SetActive(false);
        }
        else
        {
            _BtnLeft.gameObject.SetActive(true);
        }

        if (_ShowingIdx == _ValidImageCnt - 1)
        {
            _BtnRight.gameObject.SetActive(false);
        }
        else
        {
            _BtnRight.gameObject.SetActive(true);
        }
    }

    public void OnBtnLeft()
    {
        --_ShowingIdx;

        _ShowingIdx = Mathf.Max(_ShowingIdx, 0);

        RefreshShow();
    }

    public void OnBtnRight()
    {
        ++_ShowingIdx;

        _ShowingIdx = Mathf.Min(_ShowingIdx, _ValidImageCnt - 1);

        RefreshShow();
    }

    public void CloseWindow()
    {
        gameObject.SetActive(false);
    }

    #region loading effect

    public GameObject _EffectLoadingRes;

    private GameObject _EffectLoadingResInstance;

    private void InitEffet()
    {
        if (_EffectLoadingResInstance == null)
        {
            _EffectLoadingResInstance = GameObject.Instantiate(_EffectLoadingRes);
            _EffectLoadingResInstance.transform.SetParent(transform);
            _EffectLoadingResInstance.transform.localScale = Vector3.one;
            _EffectLoadingResInstance.AddComponent<UIParticleSystem>();
        }
    }

    public void UpdateLoadingEffect()
    {
        InitEffet();

        if (_Images[_ShowingIdx]._Image.enabled)
        {
            _EffectLoadingResInstance.gameObject.SetActive(false);
        }
        else
        {
            _EffectLoadingResInstance.gameObject.SetActive(true);
        }
    }

    #endregion
}