using System.Collections;
using System.Collections.Generic;
using Camera;
using UnityEngine;
using Game;
using UnityEngine;

namespace InteractiveModules
{

    public class Hallway : InteractiveModule
    {
        [SerializeField] private CameraManager cameraManager;
        [SerializeField] private float UnlockDistance;
        [SerializeField] private GameScreenAsset nextScreen;
        [SerializeField] private Sprite OtherBackground;
        private GameObject LookThrough;
        private Vector3 originalScale;
        private Vector3 initScale;
        private bool lookActive;
        private float distanceDone;
        private Vector3 mouseStart;
        private bool nextSceneAccess;
        private Sprite darkBackround;
        private bool stopDrag;

        public override void Activate(ScreenController screenController)
        {
            base.Activate(screenController);
            screenController.SetBackgroundColor(ColorType.Black);
            darkBackround = transform.Find("Background").GetComponent<SpriteRenderer>().sprite;
        }

        // Start is called before the first frame update
        void Start()
        {
            LookThrough = transform.Find("Lookthrough").gameObject;
            originalScale = transform.localScale;
            initScale = new Vector3(originalScale.x, 0f, 0f);
            lookActive = false;
            distanceDone = 0f;
            nextSceneAccess = false;
            stopDrag = false;
        }

        // Update is called once per frame
        void Update()
        {
            if (lookActive)
            {
                Vector3 pos = cameraManager.GameCamera.ScreenToWorldPoint(Input.mousePosition);
#if UNITY_ANDROID || UNITY_IOS
                LookThrough.transform.position = new Vector3(pos.x - 1.5f, pos.y + 1.5f, LookThrough.transform.position.z);
#else
                LookThrough.transform.position = new Vector3(pos.x, pos.y, LookThrough.transform.position.z);
#endif
            }
        }

        public void OnMouseDown()
        {
            if (distanceDone <= UnlockDistance)
            {
                LookThrough.SetActive(true);
                LookThrough.transform.localScale = initScale;
                LookThrough.LeanScale(originalScale, 0.3f);
                lookActive = true;

                mouseStart = cameraManager.GameCamera.ScreenToWorldPoint(Input.mousePosition);
            }
            else if (nextSceneAccess)
            {
                currentScreenController.LoadScreen(nextScreen);
            }
        }
        
        public void OnMouseDrag()
        {
            if (!stopDrag)
            {
                Vector3 mousePos = cameraManager.GameCamera.ScreenToWorldPoint(Input.mousePosition);
                distanceDone += (new Vector3(mousePos.x - mouseStart.x, mousePos.y - mouseStart.y, 0f)).magnitude;
                mouseStart = mousePos;
                if (distanceDone > UnlockDistance)
                {
                    System.Action fadeOut = delegate ()
                    {
                        transform.Find("Foreground").GetComponent<SpriteRenderer>().maskInteraction = SpriteMaskInteraction.None;
                        nextSceneAccess = true;
                    };
                    LookThrough.LeanScale(new Vector3(20 * originalScale.x, 20 * originalScale.y, originalScale.z), 3f)
                        .setOnComplete(fadeOut);
                    //transform.Find("Background").GetComponent<SpriteRenderer>().sprite = OtherBackground;
                    StartCoroutine(LightOn(transform.Find("Foreground").GetComponent<SpriteRenderer>(), 0.3f, 1.5f, 1));
                    GetComponent<AudioSource>().Play();
                    stopDrag = true;
                }
            }
        }

        public IEnumerator LightOn(SpriteRenderer renderer,float pulseHigh,float pulseLow, int count)
        {

            while (count > 0)
            {
                renderer.enabled = true;
                yield return new WaitForSeconds(pulseHigh);
                renderer.enabled = false;
                yield return new WaitForSeconds(pulseHigh);
                count--;
            }
            renderer.enabled = true;
        }

        public void OnMouseUp()
        {
            if (distanceDone <= UnlockDistance)
            {
                System.Action fadeOut = delegate ()
                {
                    LookThrough.SetActive(false);
                };
                LookThrough.LeanScale(initScale, 0.3f).setOnComplete(fadeOut);
                Debug.Log("Distance: " + distanceDone);
            }
            else
            {
                lookActive = false;
            }
        }
    }
}
