{-
	Copyright (C) 2021 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 translation between /piece/s & Unicode figurines; <https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode>.
-}

module BishBosh.Notation.Figurine(
-- * Types
-- ** Type-synonyms
--	Figurine,
-- * Constants
--	figurinesByPiece,
--	piecesByFigurine,
-- * Functions
	toFigurine,
	fromFigurine
) where

import			Data.Array.IArray((!))
import qualified	BishBosh.Component.Piece	as Component.Piece
import qualified	Data.Array.IArray
import qualified	Data.Map.Strict			as Map
import qualified	Data.Tuple

-- | A Unicode character depicting a piece.
type Figurine	= Char

-- | The constant Unicode by which to depict each piece.
figurinesByPiece :: Component.Piece.ArrayByPiece Figurine
figurinesByPiece :: ArrayByPiece Figurine
figurinesByPiece	= [Figurine] -> ArrayByPiece Figurine
forall (a :: * -> * -> *) e. IArray a e => [e] -> a Piece e
Component.Piece.listArrayByPiece [Figurine]
"\x265F\x265C\x265E\x265D\x265B\x265A\x2659\x2656\x2658\x2657\x2655\x2654"

-- | Returns the Unicode-character for the specified piece.
toFigurine :: Component.Piece.Piece -> Figurine
toFigurine :: Piece -> Figurine
toFigurine	= (ArrayByPiece Figurine
figurinesByPiece ArrayByPiece Figurine -> Piece -> Figurine
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
!)

-- | The constant piece corresponding to each Unicode figurine.
piecesByFigurine :: Map.Map Figurine Component.Piece.Piece
piecesByFigurine :: Map Figurine Piece
piecesByFigurine	= [(Figurine, Piece)] -> Map Figurine Piece
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(Figurine, Piece)] -> Map Figurine Piece)
-> ([(Piece, Figurine)] -> [(Figurine, Piece)])
-> [(Piece, Figurine)]
-> Map Figurine Piece
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Piece, Figurine) -> (Figurine, Piece))
-> [(Piece, Figurine)] -> [(Figurine, Piece)]
forall a b. (a -> b) -> [a] -> [b]
map (Piece, Figurine) -> (Figurine, Piece)
forall a b. (a, b) -> (b, a)
Data.Tuple.swap ([(Piece, Figurine)] -> Map Figurine Piece)
-> [(Piece, Figurine)] -> Map Figurine Piece
forall a b. (a -> b) -> a -> b
$ ArrayByPiece Figurine -> [(Piece, Figurine)]
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> [(i, e)]
Data.Array.IArray.assocs ArrayByPiece Figurine
figurinesByPiece

-- | Returns any piece which corresponds to the specified Unicode character.
fromFigurine :: Figurine -> Maybe Component.Piece.Piece
fromFigurine :: Figurine -> Maybe Piece
fromFigurine	= (Figurine -> Map Figurine Piece -> Maybe Piece
forall k a. Ord k => k -> Map k a -> Maybe a
`Map.lookup` Map Figurine Piece
piecesByFigurine)