using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Prisma.Main
{
    public class Pad : MonoBehaviour
    {

        [SerializeField]
        private GameObject[] door;
        [SerializeField]
        private GameObject pad;
        public static bool padact;
        [SerializeField]
        private GameObject wave;
        [SerializeField]
        private GameObject hwave;
        private int scaleX;
        private int scaleY;
        private int speedX;
        private bool roof;
        [SerializeField]
        private AudioClip[] clips;
        private AudioSource source;
        [SerializeField]
        private Texture[] waves;
        private Texture selectedTex;

        void Start()
        {
            source = GetComponent<AudioSource>();
            GameMngr.onLeft += OnLeft;
            GameMngr.onRight += OnRight;
            GameMngr.onUp += OnUp;
            GameMngr.onDown += OnDown;
            GameMngr.onD += OnD;
            GameMngr.onS += OnS;
            GameMngr.onPad += OnPad;
            PadReset();
            roof = false;
        }

        public void PadReset()
        {
            selectedTex = waves[UnityEngine.Random.Range(0, 3)];
            wave.GetComponent<Renderer>().material.SetTexture("_MainTex", selectedTex);
            hwave.GetComponent<Renderer>().material.SetTexture("_MainTex", selectedTex);
            //Ref mat
            scaleX = UnityEngine.Random.Range(1, 5);
            scaleY = UnityEngine.Random.Range(1, 5);
            speedX = UnityEngine.Random.Range(1, 5);
            wave.GetComponent<Renderer>().material.SetTextureScale("_MainTex", new Vector2(scaleX, scaleY));
            wave.GetComponent<Renderer>().material.SetInt("_ScrollXSpeed", speedX);
            //Player mat
            padact = false;
            scaleX = UnityEngine.Random.Range(1, 5);
            scaleY = UnityEngine.Random.Range(1, 5);
            speedX = UnityEngine.Random.Range(1, 5);
            hwave.GetComponent<Renderer>().material.SetTextureScale("_MainTex", new Vector2(scaleX, scaleY));
            hwave.GetComponent<Renderer>().material.SetInt("_ScrollXSpeed", speedX);
        }

        public bool RPadAct()
        {
            return padact;
        }

        private void OnS()
        {
            if (padact)
            {
                if (speedX > 1)
                    speedX = speedX - 1;
                else
                    speedX = 5;
                hwave.GetComponent<Renderer>().material.SetInt("_ScrollXSpeed", speedX);
            }
        }

        private void OnD()
        {
            if (padact)
            {
                if (speedX < 5)
                    speedX = speedX + 1;
                else
                    speedX = 1;
                hwave.GetComponent<Renderer>().material.SetInt("_ScrollXSpeed", speedX);
            }
        }

        private void OnDown()
        {
            if (padact) {
                if (scaleY > 1)
                    scaleY = scaleY - 1;
                else
                    scaleY = 5;
                hwave.GetComponent<Renderer>().material.SetTextureScale("_MainTex", new Vector2(scaleX, scaleY));
            }
        }

        private void OnUp()
        {
            if (padact)
            {
                if (scaleY < 5)
                    scaleY = scaleY + 1;
                else
                    scaleY = 1;
                hwave.GetComponent<Renderer>().material.SetTextureScale("_MainTex", new Vector2(scaleX, scaleY));
            }
        }

        private void OnRight()
        {
            if (padact)
            {
                if (scaleX < 5)
                    scaleX = scaleX + 1;
                else
                    scaleX = 1;
                hwave.GetComponent<Renderer>().material.SetTextureScale("_MainTex", new Vector2(scaleX, scaleY));
            }
        }

        private void OnLeft()
        {
            if (padact)
            {
                if (scaleX > 1)
                    scaleX = scaleX - 1;
                else
                    scaleX = 5;
                hwave.GetComponent<Renderer>().material.SetTextureScale("_MainTex", new Vector2(scaleX, scaleY));
            }
        }

        private void OnPad()
        {
            if (pad.activeSelf)
            {
                StartCoroutine(Check());
                pad.SetActive(false);
            }
            else
            {
                pad.SetActive(true);
                source.clip = clips[UnityEngine.Random.Range(2, 5)];
                source.Play();
            }
            padact = pad.activeSelf;
        }

        IEnumerator Check()
        {
            source.Stop();
            if (hwave.GetComponent<Renderer>().material.GetInt("_ScrollYSpeed") == wave.GetComponent<Renderer>().material.GetInt("_ScrollYSpeed") && hwave.GetComponent<Renderer>().material.GetInt("_ScrollXSpeed") == wave.GetComponent<Renderer>().material.GetInt("_ScrollXSpeed") && hwave.GetComponent<Renderer>().material.GetTextureScale("_MainTex") == wave.GetComponent<Renderer>().material.GetTextureScale("_MainTex"))
            {
                source.clip = clips[1];
                door[gameObject.GetComponent<GameMngr>().RLevel() - 1].GetComponent<AudioSource>().Play();
                switch (GetComponent<GameMngr>().RLevel())
                {
                    case 1:
                        door[gameObject.GetComponent<GameMngr>().RLevel() - 1].GetComponent<Animator>().SetTrigger("Open");
                        break;
                    case 2:
                        door[gameObject.GetComponent<GameMngr>().RLevel() - 1].GetComponent<Animator>().SetTrigger("Open2");
                        break;
                    case 3:
                        door[gameObject.GetComponent<GameMngr>().RLevel() - 1].GetComponent<Animator>().SetTrigger("Open3");
                        roof = true;
                        break;
                }
            }
            else
                source.clip = clips[0];
            source.loop = false;
            source.Play();
            yield return new WaitForSeconds(0.5f);
            source.Stop();
        }

        public bool RRoof()
        {
            return roof;
        }
    }
}