using System.Collections.Generic;
using System.Linq;
using Enums;
using Models.Interfaces;
using Services.Interfaces;
using UnityEngine;

namespace Services {
    public class MatchService : IMatchService {
        private List<Gem> currentMatches = new List<Gem>();
        public List<Gem> CurrentMatches => this.currentMatches;
        
        private IGameBoard gameBoard;

        public MatchService(IGameBoard gameBoard) {
            this.gameBoard = gameBoard;
        }
        
        public bool MatchesAt(Vector2Int positionToCheck, GemType gemTypeToCheck) {
            Gem[,] gems = this.gameBoard.GemsGrid;
            if (positionToCheck.x > 1)
            {
                if (gems[positionToCheck.x - 1, positionToCheck.y].Type == gemTypeToCheck && gems[positionToCheck.x - 2, positionToCheck.y].Type == gemTypeToCheck)
                    return true;
            }

            if (positionToCheck.y > 1)
            {
                if (gems[positionToCheck.x, positionToCheck.y - 1].Type == gemTypeToCheck && gems[positionToCheck.x, positionToCheck.y - 2].Type == gemTypeToCheck)
                    return true;
            }

            return false;
        }

        public void FindAllMatches() {
            this.currentMatches.Clear();

            for (int x = 0; x < this.gameBoard.Width; x++)
                for (int y = 0; y < this.gameBoard.Height; y++)
                {
                    Gem currentGem = this.gameBoard.GemsGrid[x, y];
                    if (currentGem != null)
                    {
                        if (x > 0 && x < this.gameBoard.Width - 1)
                        {
                            Gem leftGem = this.gameBoard.GemsGrid[x - 1, y];
                            Gem rightGem = this.gameBoard.GemsGrid[x + 1, y];
                            //checking no empty spots
                            if (leftGem != null && rightGem != null)
                            {
                                //Match
                                if (leftGem.Type == currentGem.Type && rightGem.Type == currentGem.Type)
                                {
                                    currentGem.isMatch = true;
                                    leftGem.isMatch = true;
                                    rightGem.isMatch = true;
                                    this.currentMatches.Add(currentGem);
                                    this.currentMatches.Add(leftGem);
                                    this.currentMatches.Add(rightGem);
                                }
                            }
                        }

                        if (y > 0 && y < this.gameBoard.Height - 1)
                        {
                            Gem aboveGem = this.gameBoard.GemsGrid[x, y - 1];
                            Gem bellowGem = this.gameBoard.GemsGrid[x, y + 1];
                            //checking no empty spots
                            if (aboveGem != null && bellowGem != null)
                            {
                                //Match
                                if (aboveGem.Type == currentGem.Type && bellowGem.Type == currentGem.Type)
                                {
                                    currentGem.isMatch = true;
                                    aboveGem.isMatch = true;
                                    bellowGem.isMatch = true;
                                    this.currentMatches.Add(currentGem);
                                    this.currentMatches.Add(aboveGem);
                                    this.currentMatches.Add(bellowGem);
                                }
                            }
                        }
                    }
                }

            if (this.currentMatches.Count > 0) this.currentMatches = this.currentMatches.Distinct().ToList();

            CheckForBombs();
        }

        public void CheckForBombs() {
            throw new System.NotImplementedException();
        }

        public void MarkBombArea(Vector2Int bombPosition, int blastSize) {
            throw new System.NotImplementedException();
        }
    }
}