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

namespace Prisma.Main
{
    public class GuardiaSc : MonoBehaviour
    {
        [SerializeField]
        private Transform pointA;
        [SerializeField]
        private Transform pointB;
        [SerializeField]
        private Transform IPos;
        public Transform target;
        public bool warn;
        public bool endwarn;
        public bool checking;
        public bool attack;
        [SerializeField]
        private float walksp = 0.2f;
        private Animator anim;
        private SpriteRenderer rend;
        [SerializeField]
        private AudioClip[] fx;
        private AudioSource source;
        private bool alert;

        private void Start()
        {
            alert = false;
            attack = false;
            target = pointA;
            warn = false;
            checking = false;
            anim = GetComponent<Animator>();
            rend = GetComponent<SpriteRenderer>();
            source = GetComponent<AudioSource>();
        }

        public void RestorePos()
        {
            gameObject.transform.position = IPos.position;
        }

        public void ResG()
        {
            anim.SetBool("Attack", false);
            anim.SetBool("Walking", false);
            anim.SetBool("Idle", true);
            alert = false;
            attack = false;
            target = pointA;
            warn = false;
            endwarn = true;
            checking = false;
        }

        public Transform Target()
        {            
            return target;
        }

        public bool Warn()
        {
            return warn;
        }

        public bool REndWarn()
        {
            return endwarn;
        }

        public bool RAttack()
        {
            return attack;
        }

        public void ChangeTarget()
        {
            if (target == pointA)
                target = pointB;
            else
                target = pointA;
        }

        public void Reach()
        {
            if (!checking)
            {
                StartCoroutine(Check());                
            }
        }

        public bool RCheck()
        {
            return checking;
        }

        private IEnumerator Check()
        {
            checking = true;
            rend.flipX = true;
            yield return new WaitForSeconds(0.25f);
            rend.flipX = false;
            yield return new WaitForSeconds(0.25f);
            rend.flipX = true;
            yield return new WaitForSeconds(0.25f);
            rend.flipX = false;
            yield return new WaitForSeconds(0.25f);
            checking = false;
            ChangeTarget();
            source.loop = false;
            source.Stop();
            alert = false;
            endwarn = false;
        }

        private void OnTriggerEnter2D(Collider2D trig)
        {
            if (trig.gameObject.name == "Sound" && GameMngr.rush == 1 && !alert) //
            {
                source.clip = fx[0];
                source.Play();
                alert = true;
                warn = true;
                anim.SetTrigger("Warn");
            }
        }

        private void OnCollisionEnter2D(Collision2D col)
        {
            if (col.gameObject.name == "Thief")
            {                
                attack = true;
            }
        }

        public void EndWarn()
        {
            source.clip = fx[1];
            source.loop = true;
            source.Play();
            warn = false;
            endwarn = true;
        }

    }
}