using Dev.Scripts.Utilities;
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.WSA;

public class MovableChild : MonoBehaviour, IAttachable
{
    [SerializeField] internal SpriteBundleScriptableObject spriteBundle;
    [SerializeField] internal List<ShapeColliderModel> _colliderList;
    [SerializeField] private SpriteRenderer _renderer;
    [SerializeField] private Color _color;
    [SerializeField] private Color TransparentColor { get { return new Color(_color.r, _color.g, _color.b, 0.5f); } }
    [SerializeField] private int _index;
    [SerializeField] private Coordinate _coordinate;
    [SerializeField] private Movable _parentMovable;
    [SerializeField] private bool _triggered;
    [SerializeField] private Vector3 _positionOffset;
    private IHolder _holderTriggeredCollider;
    public IHolder HolderTriggeredCollider => _holderTriggeredCollider;
    [SerializeField] private bool _attached;
    public bool Attached => _attached;
    public void Init(int index, Coordinate coordinate, Color color, Movable parentMovable, Vector3 positionOffset)
    {
        _index = index;
        _coordinate = coordinate;
        SetColor(color);
        _parentMovable = parentMovable;
        _positionOffset = positionOffset;
        _renderer.sprite = spriteBundle.spritesByType.Find(j => j.shapeType == LevelManager.CurrentLevel.ShapeType).sprite;
        SetCollider(LevelManager.CurrentLevel.ShapeType);
        SetPosition();
    }
    private void SetPosition()
    {
        EShapeType shapeType = EShapeType.Square;
        string shapeName = LevelManager.CurrentLevel.ShapeType.ToString();
        if (shapeName.Contains("Hexagon"))
        {
            shapeType = EShapeType.Hexagon2;
        }
        Vector3 coordinatePosition = new Vector3(_coordinate.X, -_coordinate.Y * StaticBoardManager.SpaceBetweenTiles(shapeType).y);
        if (_coordinate.Y % 2 == 1)
            coordinatePosition += StaticBoardManager.GetPositionOffsetOfTileType(shapeType);
        transform.localPosition = coordinatePosition + _positionOffset;
    }
    private void SetColor(Color color)
    {
        _color = color;
        _renderer.color = _color;
    }
    public void SetRenderOrder(int order)
    {
        _renderer.sortingOrder = order;
    }
    internal virtual void SetCollider(EShapeType boardTileType)
    {
        _colliderList.ForEach(colliderData => { colliderData.collider.enabled = (colliderData.shapeType == boardTileType); });
    }
    private void OnTriggerStay2D(Collider2D col)
    {
        IHolder holder = col.gameObject.GetComponent(typeof(IHolder)) as IHolder;
        if (holder != null)
        {
            _holderTriggeredCollider = holder;
            _triggered = true;
            ((BoardTile)holder).SetColor(TransparentColor);
        }
    }
    private void OnTriggerExit2D(Collider2D col)
    {
        IHolder holder = col.gameObject.GetComponent(typeof(IHolder)) as IHolder;
        if (holder != null)
        {
            _holderTriggeredCollider = null;
            _triggered = false;
            ((BoardTile)holder).SetColor(Color.white);
        }
    }
    private void OnMouseDown()
    {
        App_EventManager.OnMovableMouseDown?.Invoke(_parentMovable);
    }
    private void OnMouseUp()
    {
        App_EventManager.OnMovableMouseUp?.Invoke(_parentMovable);
    }

    public void Attach(IHolder holder)
    {
        var boardTile = ((BoardTile)holder);
        transform.position = boardTile.transform.position;
        _attached = true;
        boardTile.OnAttach(this);
    }

    public void Deattach(IHolder holder)
    {
        var boardTile = ((BoardTile)holder);
        transform.localPosition = Vector3.zero;
        _attached = false;
        SetPosition();
        boardTile.OnDeattach(this);
    }
}
