using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//Author: SkyAllen Email: 894982165@qq.com Website: www.ytn0.com
namespace MPStudio
{
    public class ImageLightPlay : MonoBehaviour
    {
        public List<GameObject> imgList;
        public List<GameObject> barList;

        public bool IsAutoPlay = false;

        private int imgIdx = 0;

        public float cd = 3f;

        void Start()
        {
            if (imgList.Count != barList.Count)
            {

                Debug.Log("Error");
            }

            updateImgAndBar(this.imgIdx);
            if (IsAutoPlay)
            {
                AutoPlay();
            }
        }

        void AutoPlay()
        {
            IEnumerator IE()
            {
                while (true)
                {
                    yield return new WaitForSeconds(this.cd);
                    AddIdx();
                }
            }

            StartCoroutine(IE());
        }


        public void AddIdx()
        {
            imgIdx++;
            if (imgIdx >= imgList.Count)
            {
                imgIdx = 0;
            }

            updateImgAndBar(imgIdx);
        }

        public void ReduceIdx()
        {
            imgIdx--;

            if (imgIdx < 0)
            {
                imgIdx = imgList.Count - 1;
            }

            updateImgAndBar(imgIdx);
        }

        void updateImgAndBar(int idx)
        {
            foreach (GameObject img in imgList)
            {
                img.gameObject.SetActive(false);
            }

            imgList[idx].gameObject.SetActive(true);

            foreach (GameObject bar in barList)
            {
                bar.GetComponent<Image>().color = Color.white;
            }

            barList[idx].GetComponent<Image>().color = Color.red;
        }


        void Update()
        {

        }
    }
}