using Dev.Scripts.Utilities;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Linq;
using UnityEngine;
using DG.Tweening;
public class BoardTile : MonoBehaviour, IBoardTile, IHolder
{
    public BoardTileData Data;
    [SerializeField] internal SpriteBundleScriptableObject spriteBundle;
    [SerializeField] internal List<ShapeColliderModel> _colliderList;
    [SerializeField] internal SpriteRenderer _spriteRenderer;
    [SerializeField] internal Color defaultColor;
    [SerializeField] internal Color hoverColor;
    [SerializeField] internal Color selectedColor;


    private List<IAttachable> _attachedList = new List<IAttachable>();
    public List<IAttachable> AttachedList => _attachedList;
    [SerializeField] private bool _isAvailable;
    public bool IsAvailable => _attachedList.Count < 1;
    public virtual void Init(BoardTileData boardTileData)
    {
        Data = boardTileData;
        SetPosition();
        SetCollider(LevelManager.CurrentLevel.ShapeType);
        _spriteRenderer.sprite = spriteBundle.spritesByType.Find(j => j.shapeType == Data.ShapeType).sprite;
        _spriteRenderer.color = defaultColor;
    }
    internal virtual void SetCollider(EShapeType boardTileType)
    {
        _colliderList.ForEach(colliderData => { colliderData.collider.enabled = (colliderData.shapeType == boardTileType); });
    }
    internal virtual void SetPosition()
    {
        Vector3 coordinatePosition = new Vector3(Data.Coordinate.X, -Data.Coordinate.Y * StaticBoardManager.SpaceBetweenTiles(Data.ShapeType).y);
        if (Data.Coordinate.Y % 2 == 1)
            coordinatePosition += StaticBoardManager.GetPositionOffsetOfTileType(Data.ShapeType);
        transform.localPosition = coordinatePosition;
    }

    internal virtual void OnMouseOver()
    {
        if (SelectionState == ESelectionState.Selected) return;
        SetSelectionState(ESelectionState.Hovered);
        if (IsAvailable)
            SetColor(hoverColor, 0.5f);
    }
    internal virtual void OnMouseExit()
    {
        if (SelectionState == ESelectionState.Selected) return;
        SetSelectionState(ESelectionState.None);
        if (IsAvailable)
            SetColor(defaultColor, 0.5f);
    }
    internal virtual void OnMouseDown()
    {
        if (SelectionState == ESelectionState.Selected) return;
        SetSelectionState(ESelectionState.Selected);
        if (IsAvailable)
            SetColor(selectedColor, 0.5f);
    }
    internal virtual void OnMouseDrag()
    {
        SetMovementState(EMovementState.Dragging);
    }
    internal virtual void SetSelectionState(ESelectionState state)
    {
        SelectionState = state;
        Data.SelectionState = (int)SelectionState;
    }
    internal virtual void SetMovementState(EMovementState state)
    {
        MovementState = state;
    }

    internal virtual void SetColor(Color color, float duration = 0f)
    {
        _spriteRenderer.DOColor(color, duration);
    }
    public void OnAttach(IAttachable attachable)
    {
        AttachedList.Add(attachable);
        SetColor(defaultColor, 0.5f);
    }
    public void OnDeattach(IAttachable attachable)
    {
        AttachedList.Remove(attachable);
        SetColor(defaultColor, 0.5f);
    }

    public enum ESelectionState
    {
        None,
        Hovered,
        Selected,
    }
    public ESelectionState SelectionState;
    public enum EMovementState
    {
        None,
        Dragging
    }
    public EMovementState MovementState;
}
