using Dev.Scripts.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using UnityEngine;

public class BoardManager : MonoBehaviour
{
    [Header("Instantiated Prefab Lists")]
    public List<BoardTile> boardTiles;

    [Header("References")]
    [SerializeField] internal CameraManager _cameraManager;
    [SerializeField] internal BoardTile _boardTilePrefab;
    [SerializeField] internal Transform _boardTileHolderTransform;

    [SerializeField] internal BoardData _data;

    internal virtual void OnEnable()
    {
        CreateBoard();
    }
    internal virtual void CreateBoard()
    {
        LevelManager.Init();
        _data = LevelManager.CurrentLevel.BoardData;
        _cameraManager.SetOrthographicSize();
        _cameraManager.SetPosition();
        CreateTiles();
        App_EventManager.OnBoardCreated?.Invoke(_data);
    }

    internal virtual void Update()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            ResetScene();
        }
    }
    internal virtual void CreateTiles()
    {
        DestroyBoardTiles();

        for (int x = 0; x < _data.Size.x; x++)
        {
            for (int y = 0; y < _data.Size.y; y++)
            {
                Coordinate coordinate = new Coordinate(x, y);
                if (LevelManager.CurrentLevel.ShapeType == EShapeType.Hexagon1)
                {
                    float distanceFromMiddle = Mathf.Abs((int)(_data.Size.y / 2) - coordinate.Y);
                    if (!IsOutOfRowLimitForHexagon1(coordinate, distanceFromMiddle))
                    {
                        coordinate.X += distanceFromMiddle * 0.5f;
                        CreateTile(coordinate);
                    }
                }
                else if (LevelManager.CurrentLevel.ShapeType == EShapeType.Hexagon2)
                {
                    CreateTile(coordinate);
                }
                else
                {
                    CreateTile(coordinate);
                }
            }
        }
    }
    internal virtual IBoardTile CreateTile(Coordinate coordinate)
    {
        var boardTile = Instantiate(_boardTilePrefab, _boardTileHolderTransform);
        BoardTileData boardTile_Data = new BoardTileData($"{coordinate.X}{coordinate.Y}", LevelManager.CurrentLevel.ShapeType, (int)BoardTile.ESelectionState.None, coordinate, true, false, true);
        boardTile.Init(boardTile_Data);
        boardTiles.Add(boardTile);
        return boardTile;
    }
    private bool IsOutOfRowLimitForHexagon1(Coordinate coordinate, float distanceFromMiddle)
    {
        return _data.Size.x - distanceFromMiddle <= coordinate.X;
    }
    internal virtual void DestroyBoardTiles()
    {
        if (boardTiles == null)
            return;
        if (boardTiles.Count == 0)
            return;
        for (int i = boardTiles.Count - 1; i >= 0; i--)
        {
            Destroy(boardTiles[i].gameObject);
        }
        boardTiles.Clear();
    }
    internal virtual void ResetScene()
    {
        CreateBoard();
    }
}
