using System;
using DG.Tweening;
using UnityEngine;

public enum PlayerState
{
    InHome,
    InSafeZone,
    Moving,
    InFinishingPath,
    HasFinished
}

public class PlayerPawn : MonoBehaviour
{
    [SerializeField] private PlayerState playerState;

    private bool canSelectPlayerFromHome;
    public int CurrentTileIndex
    {
        get; private set;
    }

    public int PlayerId
    {
        get; private set;
    }

    public int BaseId
    {
        get; private set;
    }

    private GameplayManager gameplayManager;

    public void SetPlayerSelectionState(bool state)
    {
        canSelectPlayerFromHome = state;
        if (canSelectPlayerFromHome)
        {
            // Play animation for selecting any of the players
        }
    }

    public void SetCurrentTileIndex(int tileIndex)
    {
        CurrentTileIndex = tileIndex;
    }

    public void MoveToTile(Vector3 startingPoint, Action onComplete, int tileIndex)
    {
        CurrentTileIndex = tileIndex;
        transform.DOMove(startingPoint, 0.1f).onComplete = () => onComplete?.Invoke();
    }

    public void MoveToCustomTilePosition(Vector3 targetPoint)
    {
        transform.DOMove(targetPoint, 0.1f);
    }

    // when he is defeated
    public void MoveBackToHome()
    {
        
    }

    public PlayerState GetPlayerState() => playerState;

    public void SetPlayerState(PlayerState state)
    {
        playerState = state;
    }

    private void OnMouseDown()
    {
        if (!canSelectPlayerFromHome) return;

        SetGameplayManager();
        gameplayManager.OnPawnSelected(this);
    }

    private void SetGameplayManager()
    {
        gameplayManager = gameplayManager ?? InterfaceManager.Instance.GetInterfaceInstance<GameplayManager>();
    }

    public void InitId(int id)
    {
        PlayerId = id;
    }
}
