{-
	Copyright (C) 2018 Dr. Alistair Ward

	This file is part of BishBosh.

	BishBosh is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	BishBosh is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with BishBosh.  If not, see <http://www.gnu.org/licenses/>.
-}
{- |
 [@AUTHOR@]	Dr. Alistair Ward

 [@DESCRIPTION@]	Defines the relative value of a specific rank of piece, occupying a specific coordinate on the board, at a specific stage in the game.
-}

module BishBosh.Component.PieceSquareArray(
-- * Types
-- ** Type-synonyms
	InterpolatedPieceSquareValues,
--	InterpolatedPieceSquareValuesByCoordinates,
	FindPieceSquareValue,
-- ** Data-types
	PieceSquareArray,
-- * Functions
	findPieceSquareValue,
-- ** Constructor
	mkPieceSquareArray
) where

import			BishBosh.Data.Bool()
import			Data.Array.IArray((!))
import qualified	BishBosh.Attribute.LogicalColour	as Attribute.LogicalColour
import qualified	BishBosh.Attribute.Rank			as Attribute.Rank
import qualified	BishBosh.Cartesian.Coordinates		as Cartesian.Coordinates
import qualified	BishBosh.Component.Piece		as Component.Piece
import qualified	BishBosh.Property.Reflectable		as Property.Reflectable
import qualified	Control.DeepSeq
import qualified	Data.Array.IArray

-- | Either a single /pieceSquareValue/ or an array of values indexed by the total number of pieces remaining on the board.
type InterpolatedPieceSquareValues pieceSquareValue	= Either pieceSquareValue (Data.Array.IArray.Array Component.Piece.NPieces pieceSquareValue)

-- | Self-documentation.
type InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue	= Cartesian.Coordinates.ByCoordinates x y (InterpolatedPieceSquareValues pieceSquareValue)

-- | The value for each type of /piece/ of occupying each coordinate, at each stage in the lifetime of the game.
newtype PieceSquareArray x y pieceSquareValue		= MkPieceSquareArray {
	PieceSquareArray x y pieceSquareValue
-> ByRank
     (InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
deconstruct	:: Attribute.Rank.ByRank (InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
} deriving (PieceSquareArray x y pieceSquareValue
-> PieceSquareArray x y pieceSquareValue -> Bool
(PieceSquareArray x y pieceSquareValue
 -> PieceSquareArray x y pieceSquareValue -> Bool)
-> (PieceSquareArray x y pieceSquareValue
    -> PieceSquareArray x y pieceSquareValue -> Bool)
-> Eq (PieceSquareArray x y pieceSquareValue)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall x y pieceSquareValue.
(Enum x, Enum y, Ord x, Ord y, Eq pieceSquareValue) =>
PieceSquareArray x y pieceSquareValue
-> PieceSquareArray x y pieceSquareValue -> Bool
/= :: PieceSquareArray x y pieceSquareValue
-> PieceSquareArray x y pieceSquareValue -> Bool
$c/= :: forall x y pieceSquareValue.
(Enum x, Enum y, Ord x, Ord y, Eq pieceSquareValue) =>
PieceSquareArray x y pieceSquareValue
-> PieceSquareArray x y pieceSquareValue -> Bool
== :: PieceSquareArray x y pieceSquareValue
-> PieceSquareArray x y pieceSquareValue -> Bool
$c== :: forall x y pieceSquareValue.
(Enum x, Enum y, Ord x, Ord y, Eq pieceSquareValue) =>
PieceSquareArray x y pieceSquareValue
-> PieceSquareArray x y pieceSquareValue -> Bool
Eq, Int -> PieceSquareArray x y pieceSquareValue -> ShowS
[PieceSquareArray x y pieceSquareValue] -> ShowS
PieceSquareArray x y pieceSquareValue -> String
(Int -> PieceSquareArray x y pieceSquareValue -> ShowS)
-> (PieceSquareArray x y pieceSquareValue -> String)
-> ([PieceSquareArray x y pieceSquareValue] -> ShowS)
-> Show (PieceSquareArray x y pieceSquareValue)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall x y pieceSquareValue.
(Enum x, Enum y, Ord x, Ord y, Show x, Show y,
 Show pieceSquareValue) =>
Int -> PieceSquareArray x y pieceSquareValue -> ShowS
forall x y pieceSquareValue.
(Enum x, Enum y, Ord x, Ord y, Show x, Show y,
 Show pieceSquareValue) =>
[PieceSquareArray x y pieceSquareValue] -> ShowS
forall x y pieceSquareValue.
(Enum x, Enum y, Ord x, Ord y, Show x, Show y,
 Show pieceSquareValue) =>
PieceSquareArray x y pieceSquareValue -> String
showList :: [PieceSquareArray x y pieceSquareValue] -> ShowS
$cshowList :: forall x y pieceSquareValue.
(Enum x, Enum y, Ord x, Ord y, Show x, Show y,
 Show pieceSquareValue) =>
[PieceSquareArray x y pieceSquareValue] -> ShowS
show :: PieceSquareArray x y pieceSquareValue -> String
$cshow :: forall x y pieceSquareValue.
(Enum x, Enum y, Ord x, Ord y, Show x, Show y,
 Show pieceSquareValue) =>
PieceSquareArray x y pieceSquareValue -> String
showsPrec :: Int -> PieceSquareArray x y pieceSquareValue -> ShowS
$cshowsPrec :: forall x y pieceSquareValue.
(Enum x, Enum y, Ord x, Ord y, Show x, Show y,
 Show pieceSquareValue) =>
Int -> PieceSquareArray x y pieceSquareValue -> ShowS
Show)

instance (
	Control.DeepSeq.NFData	pieceSquareValue,
	Control.DeepSeq.NFData	x,
	Control.DeepSeq.NFData	y
 ) => Control.DeepSeq.NFData (PieceSquareArray x y pieceSquareValue) where
	rnf :: PieceSquareArray x y pieceSquareValue -> ()
rnf MkPieceSquareArray { deconstruct :: forall x y pieceSquareValue.
PieceSquareArray x y pieceSquareValue
-> ByRank
     (InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
deconstruct = ByRank
  (InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
byRank }	= ByRank
  (InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
-> ()
forall a. NFData a => a -> ()
Control.DeepSeq.rnf ByRank
  (InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
byRank

-- | Constructor.
mkPieceSquareArray :: (Attribute.Rank.Rank -> InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue) -> PieceSquareArray x y pieceSquareValue
mkPieceSquareArray :: (Rank
 -> InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
-> PieceSquareArray x y pieceSquareValue
mkPieceSquareArray	= ByRank
  (InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
-> PieceSquareArray x y pieceSquareValue
forall x y pieceSquareValue.
ByRank
  (InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
-> PieceSquareArray x y pieceSquareValue
MkPieceSquareArray (ByRank
   (InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
 -> PieceSquareArray x y pieceSquareValue)
-> ((Rank
     -> InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
    -> ByRank
         (InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue))
-> (Rank
    -> InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
-> PieceSquareArray x y pieceSquareValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue]
-> ByRank
     (InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
forall (a :: * -> * -> *) e. IArray a e => [e] -> a Rank e
Attribute.Rank.listArrayByRank ([InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue]
 -> ByRank
      (InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue))
-> ((Rank
     -> InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
    -> [InterpolatedPieceSquareValuesByCoordinates
          x y pieceSquareValue])
-> (Rank
    -> InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
-> ByRank
     (InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Rank
 -> InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
-> [Rank]
-> [InterpolatedPieceSquareValuesByCoordinates
      x y pieceSquareValue]
forall a b. (a -> b) -> [a] -> [b]
`map` [Rank]
Attribute.Rank.range)

-- | The type of a function which can find the appropriate piece-square value.
type FindPieceSquareValue x y pieceSquareValue
	= Attribute.LogicalColour.LogicalColour		-- ^ The piece's logical colour.
	-> Attribute.Rank.Rank				-- ^ The piece's rank.
	-> Cartesian.Coordinates.Coordinates x y	-- ^ The piece's location.
	-> pieceSquareValue

-- | Find the piece-square value, at a stage in the game's lifetime defined by the total number of piece remaining, for the specified /piece/ & /coordinates/.
findPieceSquareValue :: (
	Enum	x,
	Enum	y,
	Ord	x,
	Ord	y
 )
	=> Component.Piece.NPieces			-- ^ The progress through the game.
	-> Attribute.LogicalColour.LogicalColour	-- ^ The piece's logical colour.
	-> Attribute.Rank.Rank				-- ^ The piece's rank.
	-> Cartesian.Coordinates.Coordinates x y	-- ^ The piece's location.
	-> PieceSquareArray x y pieceSquareValue
	-> pieceSquareValue
-- {-# SPECIALISE findPieceSquareValue :: Component.Piece.NPieces -> Attribute.LogicalColour.LogicalColour -> Attribute.Rank.Rank -> Cartesian.Coordinates.Coordinates T.X T.Y -> PieceSquareArray T.X T.Y pieceSquareValue -> pieceSquareValue #-}
{-# INLINE findPieceSquareValue #-}
findPieceSquareValue :: Int
-> LogicalColour
-> Rank
-> Coordinates x y
-> PieceSquareArray x y pieceSquareValue
-> pieceSquareValue
findPieceSquareValue Int
nPieces LogicalColour
logicalColour Rank
rank Coordinates x y
coordinates MkPieceSquareArray { deconstruct :: forall x y pieceSquareValue.
PieceSquareArray x y pieceSquareValue
-> ByRank
     (InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
deconstruct = ByRank
  (InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
byRank }	= (pieceSquareValue -> pieceSquareValue)
-> (Array Int pieceSquareValue -> pieceSquareValue)
-> Either pieceSquareValue (Array Int pieceSquareValue)
-> pieceSquareValue
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either pieceSquareValue -> pieceSquareValue
forall a. a -> a
id (Array Int pieceSquareValue -> Int -> pieceSquareValue
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! Int
nPieces) (Either pieceSquareValue (Array Int pieceSquareValue)
 -> pieceSquareValue)
-> Either pieceSquareValue (Array Int pieceSquareValue)
-> pieceSquareValue
forall a b. (a -> b) -> a -> b
$ ByRank
  (InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
byRank ByRank
  (InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue)
-> Rank
-> InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! Rank
rank InterpolatedPieceSquareValuesByCoordinates x y pieceSquareValue
-> Coordinates x y
-> Either pieceSquareValue (Array Int pieceSquareValue)
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! (
	if LogicalColour -> Bool
Attribute.LogicalColour.isBlack LogicalColour
logicalColour
		then Coordinates x y -> Coordinates x y
forall a. ReflectableOnX a => a -> a
Property.Reflectable.reflectOnX
		else Coordinates x y -> Coordinates x y
forall a. a -> a
id
 ) Coordinates x y
coordinates