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

namespace Prisma.Main
{
    public class Player : MonoBehaviour
    {

        [SerializeField]
        private float movspeed = 0.3f;
        private bool run;
        private bool hidden;
        public bool hidok;
        public bool panel;
        private Rigidbody2D rig;
        private Collider2D colplayer;
        private SpriteRenderer rend;
        private Animator anim;
        [SerializeField]
        private Animator sanim;
        private int hidlay;
        [SerializeField]
        private Transform elevatorT;
        public static bool elevatorB;        
        [SerializeField]
        private AudioClip[] fx;
        private AudioSource source;
        [SerializeField]
        private GameObject mngr;
        [SerializeField]
        private GameObject[] cameras;
        private bool dead;
        private bool GGPrisma;
        [SerializeField]
        private RuntimeAnimatorController playeranim;
        [SerializeField]
        private RuntimeAnimatorController winanim;
        [SerializeField]
        private GameObject prisma;
        [SerializeField]
        private AudioClip[] victory;
        [SerializeField]
        private Animator canvasc;

        void Start()
        {
            GameMngr.onRight += OnRight;
            GameMngr.onLeft += OnLeft;
            GameMngr.onRunOn += OnRunOn;
            GameMngr.onRunOff += OnRunOff;
            GameMngr.onHid += OnHid;
            GetComponent<Animator>().runtimeAnimatorController = playeranim as RuntimeAnimatorController;
            rig = gameObject.GetComponent<Rigidbody2D>();
            colplayer = gameObject.GetComponent<Collider2D>();
            rend = gameObject.GetComponent<SpriteRenderer>();
            anim = gameObject.GetComponent<Animator>();
            source = gameObject.GetComponent<AudioSource>();
            anim.SetBool("Dead", false);
            panel = false;
            dead = false;
            GGPrisma = false;
            prisma.SetActive(true);
        }

        private void OnHid()
        {
            if (!GGPrisma)
            {
                if (!Pad.padact)
                {
                    if (hidok && !hidden)
                    {
                        hidden = true;
                        rig.isKinematic = true;
                        colplayer.enabled = false;
                        rend.color = new Color(0.5f, 0.5f, 0.5f);
                        rend.sortingOrder = hidlay;
                        anim.SetBool("Hiding", true);
                    }
                    else
                    {
                        hidden = false;
                        rig.isKinematic = false;
                        colplayer.enabled = true;
                        rend.color = new Color(1.0f, 1.0f, 1.0f);
                        rend.sortingOrder = 0;
                        anim.SetBool("Hiding", false);
                    }
                }
            }
        }

        private void OnRunOff()
        {
            if (GameMngr.playeract && !Pad.padact)
            {
                run = false;
                anim.SetBool("Running", false);
            }
        }

        private void OnRunOn()
        {
            if (!GGPrisma)
            {
                if (GameMngr.playeract && !Pad.padact)
                {
                    run = true;
                    anim.SetBool("Running", true);
                }
            }
        }

        private void OnLeft()
        {
            if (!GGPrisma)
            {
                if (GameMngr.playeract && !run && !hidden && !Pad.padact)
                {
                    StartCoroutine(TurnL(1));
                    anim.SetTrigger("Walking");
                    sanim.SetTrigger("Walk");
                }
                else if (GameMngr.playeract && run && !hidden && !Pad.padact)
                {
                    StartCoroutine(TurnL(2));
                    sanim.SetTrigger("Run");
                }
                gameObject.transform.position = new Vector2(Mathf.Clamp(gameObject.transform.position.x, -4.0f, 4.5f), gameObject.transform.position.y);
                rend.flipX = true;
            }
        }

        private IEnumerator TurnL(int v)
        {
            gameObject.transform.Translate(Vector2.left * movspeed * v * Time.deltaTime);
            yield return new WaitForSeconds(0.5f);
        }

        private IEnumerator TurnR(int v)
        {
            gameObject.transform.Translate(Vector2.right * movspeed * v * Time.deltaTime);
            yield return new WaitForSeconds(0.5f);
        }

        private void OnRight()
        {
            if (!GGPrisma)
            {
                if (GameMngr.playeract && !run && !hidden && !Pad.padact)
                {
                    StartCoroutine(TurnR(1));
                    anim.SetTrigger("Walking");
                    sanim.SetTrigger("Walk");
                }
                else if (GameMngr.playeract && run && !hidden && !Pad.padact)
                {
                    StartCoroutine(TurnR(2));
                    sanim.SetTrigger("Run");
                }
                gameObject.transform.position = new Vector2(Mathf.Clamp(gameObject.transform.position.x, -4.0f, 4.5f), gameObject.transform.position.y);
                rend.flipX = false;
            }
        }

        public bool RHidOK()
        {
            return hidok;
        }
        public bool RPanel()
        {
            return panel;
        }


        private void OnTriggerEnter2D(Collider2D trig)
        {
            if (trig.gameObject.name == "Puerta" || trig.gameObject.name == "Planta")
            {
                if (trig.gameObject.name == "Puerta")
                    hidlay = -6;
                if (trig.gameObject.name == "Planta")
                    hidlay = 6;
                hidok = true;
            }
            if (trig.gameObject.name == "Panel" && trig.IsTouching(this.gameObject.GetComponent<Collider2D>()))
                panel = true;
            if (trig.gameObject.name == "Camera" && !(cameras[0].GetComponent<CameraSc>().RAlarm() || cameras[1].GetComponent<CameraSc>().RAlarm()))
            {
                mngr.GetComponent<GameMngr>().Alarm();
                cameras[0].GetComponent<CameraSc>().Alert();
                cameras[1].GetComponent<CameraSc>().Alert();
            }
            if (trig.gameObject.name == "Elevator")
            {
                elevatorB = true;
            }
        }

        private void OnTriggerExit2D(Collider2D trig)
        {
            if (trig.gameObject.name == "Puerta" || trig.gameObject.name == "Planta")
            {
                hidok = false;
                rend.sortingOrder = 0;
            }
            if (trig.gameObject.name == "Panel")
                panel = false;
            if (trig.gameObject.name == "Prisma")
            {
                this.gameObject.GetComponent<Animator>().runtimeAnimatorController = winanim as RuntimeAnimatorController;
            }
        }
        

        public void Credits()
        {
            canvasc.GetComponent<Animator>().SetTrigger("Credits");
        }

        public void Victory()
        {
            mngr.GetComponent<GameMngr>().Victory();
        }

        public void Revive()
        {
            dead = false;
            anim.SetBool("Dead", false);
            rig.isKinematic = false;
            gameObject.GetComponent<BoxCollider2D>().enabled = true;
        }

        public bool RDead()
        {
            return dead;
        }

        public bool RGGP()
        {
            return GGPrisma;
        }

        public void StopMusic()
        {
            source.Stop();
            source.loop = false;
        }

        public void Prismarob()
        {
            prisma.SetActive(false);
            source.clip = victory[0];
            source.Play();
        }

        public void WindowBr()
        {
            source.clip = victory[1];
            source.Play();
        }

        private void OnCollisionEnter2D(Collision2D col)
        {
            if (col.gameObject.tag == "Enemy")
            {
                Death();
            }
        }

        public void Death()
        {
            anim.SetBool("Dead", true);
            dead = true;
            source.clip = fx[0];
            source.Play();
            GameMngr.playeract = false;
            rig.isKinematic = true;
            gameObject.GetComponent<Collider2D>().enabled = false;
        }

    }
}