using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [SerializeField] GameController gameController;

    public float characterSpeed = 20;
    
    // Animation clips
    private const string idleAnimation = "idle";
    private const string walkAnimation = "walking";
    private const string sleepAnimation = "sleeping";
    private const string workAnimation = "working";

    private Vector3 activeClickHandlerPosition;
    private Animator animator;
    private SpriteRenderer spriteRenderer;
    private Tweener tweener;
    private GameController.State activeState = GameController.State.Neutral;

    public AudioClip normalMusic;
    public AudioClip socialMusic;
    public AudioClip sleepingMusic;
    public AudioClip keyboardSound;

    private void Start()
    {
        animator = GetComponent<Animator>();
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    private void Update()
    {
        if( activeState == GameController.State.Sleeping)
        {
            transform.position = gameController.getBedPosition();
        }
    }

    public void MoveToTable(Vector3 position, float delay=0)
    {
        MoveTo(position, workAnimation, GameController.State.Working, delay);
    }

    public void MoveToBed(Vector3 position, float delay=0)
    {
        MoveTo(position, sleepAnimation, GameController.State.Sleeping, delay);
    }

    public void MoveToDoor(Vector3 position, float delay=0)
    {
        MoveTo(position, walkAnimation, GameController.State.Socializing, delay);
    }

    public void MoveToNeutral(Vector3 position, float delay=0)
    {
        MoveTo(position, idleAnimation, GameController.State.Neutral, delay);
    }

    private void MoveTo(Vector3 position, string nextAnimation, GameController.State nextState, float delay = 0)
    {
        if (tweener != null)
        {
            tweener.Kill();
        }

        if(position.x > transform.position.x)
        {
            spriteRenderer.flipX = true;
        }
        else
        {
            spriteRenderer.flipX = false;
        }

        gameController.ChangeState(GameController.State.Neutral);
        animator.Play(walkAnimation);

        

        if (activeState == GameController.State.Sleeping || activeState == GameController.State.Socializing)
            AudioController.Instance.PlayMusic(normalMusic);
        if (activeState == GameController.State.Working)
            AudioController.Instance.StopContinousSound();
        
        if (activeState == GameController.State.Sleeping)
        {
            activeState = GameController.State.Neutral;
            transform.position += new Vector3(-2.5f, 1f);
        }
        
        tweener = transform.DOMove(position, characterSpeed * Vector2.Distance(transform.position, position)).SetEase(Ease.Linear).SetDelay(delay).OnComplete(() => {
            animator.Play(nextAnimation);
            gameController.ChangeState(nextState);
            gameController.CanInteract();
            
            activeState = nextState;
            switch (activeState)
            {
                case GameController.State.Sleeping:
                    spriteRenderer.flipX = false;
                    transform.position = gameController.getBedPosition();
                    AudioController.Instance.PlayMusic(sleepingMusic);
                    break;
                case GameController.State.Working:
                    spriteRenderer.flipX = false;
                    AudioController.Instance.PlayContinousSound(keyboardSound);
                    break;
                case GameController.State.Socializing:
                    AudioController.Instance.PlayMusic(socialMusic);
                    break;
                case GameController.State.Neutral:
                    break;
            }
            if (gameController.isDoorOpen)
            {
                gameController.CloseDoor();
            }
        });
    }
}
