{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

module ROC.ID.Location
  ( Location
  , fromLetter
  , toLetter
  , toText
  , generate
  ) where

import Control.Monad.Random.Class
  ( MonadRandom (..) )
import Data.Finitary
  ( Finitary )
import Data.Text
  ( Text )
import GHC.Generics
  ( Generic )
import ROC.ID.Language
  ( Language (..) )
import ROC.ID.Letter
  ( Letter (..) )
import ROC.ID.Utilities
  ( randomFinitary )
import Text.Read
  ( Lexeme (Ident, Symbol), Read (readPrec), lexP, parens )

-- | Represents a location, encodable within an ROC identification number.
--
-- == Location codes
--
-- Every location that can be represented in an ROC identification number has a
-- unique code:
--
-- +------+---------+-------------------+
-- | Code | Chinese | English           |
-- +======+=========+===================+
-- | @A@  | 臺北市     | Taipei City       |
-- +------+---------+-------------------+
-- | @B@  | 臺中市     | Taichung City     |
-- +------+---------+-------------------+
-- | @C@  | 基隆市     | Keelung City      |
-- +------+---------+-------------------+
-- | @D@  | 臺南市     | Tainan City       |
-- +------+---------+-------------------+
-- | @E@  | 高雄市     | Kaohsiung City    |
-- +------+---------+-------------------+
-- | @F@  | 新北市     | New Taipei City   |
-- +------+---------+-------------------+
-- | @G@  | 宜蘭縣     | Yilan County      |
-- +------+---------+-------------------+
-- | @H@  | 桃園市     | Taoyuan City      |
-- +------+---------+-------------------+
-- | @I@  | 嘉義市     | Chiayi City       |
-- +------+---------+-------------------+
-- | @J@  | 新竹縣     | Hsinchu County    |
-- +------+---------+-------------------+
-- | @K@  | 苗栗縣     | Miaoli County     |
-- +------+---------+-------------------+
-- | @L@  | 臺中縣     | Taichung County   |
-- +------+---------+-------------------+
-- | @M@  | 南投縣     | Nantou County     |
-- +------+---------+-------------------+
-- | @N@  | 彰化縣     | Changhua County   |
-- +------+---------+-------------------+
-- | @O@  | 新竹市     | Hsinchu City      |
-- +------+---------+-------------------+
-- | @P@  | 雲林縣     | Yunlin County     |
-- +------+---------+-------------------+
-- | @Q@  | 嘉義縣     | Chiayi County     |
-- +------+---------+-------------------+
-- | @R@  | 臺南縣     | Tainan County     |
-- +------+---------+-------------------+
-- | @S@  | 高雄縣     | Kaohsiung County  |
-- +------+---------+-------------------+
-- | @T@  | 屏東縣     | Pingtung County   |
-- +------+---------+-------------------+
-- | @U@  | 花蓮縣     | Hualien County    |
-- +------+---------+-------------------+
-- | @V@  | 臺東縣     | Taitung County    |
-- +------+---------+-------------------+
-- | @W@  | 金門縣     | Kinmen County     |
-- +------+---------+-------------------+
-- | @X@  | 澎湖縣     | Penghu County     |
-- +------+---------+-------------------+
-- | @Y@  | 陽明山     | Yangmingshan      |
-- +------+---------+-------------------+
-- | @Z@  | 連江縣     | Lienchiang County |
-- +------+---------+-------------------+
--
-- == Usage
--
-- To construct a 'Location' from its letter code, use the 'fromLetter'
-- function.
--
-- To print the full name of a 'Location', use the 'toText' function.
--
-- To generate a random 'Location', use the 'generate' function.
--
newtype Location = Location Letter
  deriving stock (Location -> Location -> Bool
(Location -> Location -> Bool)
-> (Location -> Location -> Bool) -> Eq Location
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Location -> Location -> Bool
== :: Location -> Location -> Bool
$c/= :: Location -> Location -> Bool
/= :: Location -> Location -> Bool
Eq, (forall x. Location -> Rep Location x)
-> (forall x. Rep Location x -> Location) -> Generic Location
forall x. Rep Location x -> Location
forall x. Location -> Rep Location x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Location -> Rep Location x
from :: forall x. Location -> Rep Location x
$cto :: forall x. Rep Location x -> Location
to :: forall x. Rep Location x -> Location
Generic, Eq Location
Eq Location =>
(Location -> Location -> Ordering)
-> (Location -> Location -> Bool)
-> (Location -> Location -> Bool)
-> (Location -> Location -> Bool)
-> (Location -> Location -> Bool)
-> (Location -> Location -> Location)
-> (Location -> Location -> Location)
-> Ord Location
Location -> Location -> Bool
Location -> Location -> Ordering
Location -> Location -> Location
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Location -> Location -> Ordering
compare :: Location -> Location -> Ordering
$c< :: Location -> Location -> Bool
< :: Location -> Location -> Bool
$c<= :: Location -> Location -> Bool
<= :: Location -> Location -> Bool
$c> :: Location -> Location -> Bool
> :: Location -> Location -> Bool
$c>= :: Location -> Location -> Bool
>= :: Location -> Location -> Bool
$cmax :: Location -> Location -> Location
max :: Location -> Location -> Location
$cmin :: Location -> Location -> Location
min :: Location -> Location -> Location
Ord)
  deriving newtype (Location
Location -> Location -> Bounded Location
forall a. a -> a -> Bounded a
$cminBound :: Location
minBound :: Location
$cmaxBound :: Location
maxBound :: Location
Bounded, Int -> Location
Location -> Int
Location -> [Location]
Location -> Location
Location -> Location -> [Location]
Location -> Location -> Location -> [Location]
(Location -> Location)
-> (Location -> Location)
-> (Int -> Location)
-> (Location -> Int)
-> (Location -> [Location])
-> (Location -> Location -> [Location])
-> (Location -> Location -> [Location])
-> (Location -> Location -> Location -> [Location])
-> Enum Location
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: Location -> Location
succ :: Location -> Location
$cpred :: Location -> Location
pred :: Location -> Location
$ctoEnum :: Int -> Location
toEnum :: Int -> Location
$cfromEnum :: Location -> Int
fromEnum :: Location -> Int
$cenumFrom :: Location -> [Location]
enumFrom :: Location -> [Location]
$cenumFromThen :: Location -> Location -> [Location]
enumFromThen :: Location -> Location -> [Location]
$cenumFromTo :: Location -> Location -> [Location]
enumFromTo :: Location -> Location -> [Location]
$cenumFromThenTo :: Location -> Location -> Location -> [Location]
enumFromThenTo :: Location -> Location -> Location -> [Location]
Enum)
  deriving anyclass Eq Location
KnownNat (Cardinality Location)
(Eq Location, KnownNat (Cardinality Location)) =>
(Finite (Cardinality Location) -> Location)
-> (Location -> Finite (Cardinality Location))
-> ((1 <= Cardinality Location) => Location)
-> ((1 <= Cardinality Location) => Location)
-> (Location -> Maybe Location)
-> (Location -> Maybe Location)
-> Finitary Location
(1 <= Cardinality Location) => Location
Finite (Cardinality Location) -> Location
Location -> Maybe Location
Location -> Finite (Cardinality Location)
forall a.
(Eq a, KnownNat (Cardinality a)) =>
(Finite (Cardinality a) -> a)
-> (a -> Finite (Cardinality a))
-> ((1 <= Cardinality a) => a)
-> ((1 <= Cardinality a) => a)
-> (a -> Maybe a)
-> (a -> Maybe a)
-> Finitary a
$cfromFinite :: Finite (Cardinality Location) -> Location
fromFinite :: Finite (Cardinality Location) -> Location
$ctoFinite :: Location -> Finite (Cardinality Location)
toFinite :: Location -> Finite (Cardinality Location)
$cstart :: (1 <= Cardinality Location) => Location
start :: (1 <= Cardinality Location) => Location
$cend :: (1 <= Cardinality Location) => Location
end :: (1 <= Cardinality Location) => Location
$cprevious :: Location -> Maybe Location
previous :: Location -> Maybe Location
$cnext :: Location -> Maybe Location
next :: Location -> Maybe Location
Finitary

instance Read Location where
  readPrec :: ReadPrec Location
readPrec = ReadPrec Location -> ReadPrec Location
forall a. ReadPrec a -> ReadPrec a
parens (ReadPrec Location -> ReadPrec Location)
-> ReadPrec Location -> ReadPrec Location
forall a b. (a -> b) -> a -> b
$ do
    Ident "Location"   <- ReadPrec Lexeme
lexP
    Symbol "."         <- lexP
    Ident "fromLetter" <- lexP
    fromLetter <$> readPrec

instance Show Location where
  showsPrec :: Int -> Location -> String -> String
showsPrec Int
_ Location
s =
    String -> String -> String
showString String
"Location.fromLetter " (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Letter -> String -> String
forall a. Show a => a -> String -> String
shows (Location -> Letter
toLetter Location
s)

-- | Constructs a 'Location' from its corresponding letter code.
--
fromLetter :: Letter -> Location
fromLetter :: Letter -> Location
fromLetter = Letter -> Location
Location

-- | Converts a 'Location' to its corresponding letter code.
--
toLetter :: Location -> Letter
toLetter :: Location -> Letter
toLetter (Location Letter
letter) = Letter
letter

-- | Prints the specified 'Location'.
toText :: Language -> Location -> Text
toText :: Language -> Location -> Text
toText = \case
  Language
English -> Location -> Text
toTextEnglish
  Language
Chinese -> Location -> Text
toTextChinese

toTextChinese :: Location -> Text
toTextChinese :: Location -> Text
toTextChinese (Location Letter
letter) = case Letter
letter of
  Letter
A -> Text
"臺北市"
  Letter
B -> Text
"臺中市"
  Letter
C -> Text
"基隆市"
  Letter
D -> Text
"臺南市"
  Letter
E -> Text
"高雄市"
  Letter
F -> Text
"新北市"
  Letter
G -> Text
"宜蘭縣"
  Letter
H -> Text
"桃園市"
  Letter
I -> Text
"嘉義市"
  Letter
J -> Text
"新竹縣"
  Letter
K -> Text
"苗栗縣"
  Letter
L -> Text
"臺中縣"
  Letter
M -> Text
"南投縣"
  Letter
N -> Text
"彰化縣"
  Letter
O -> Text
"新竹市"
  Letter
P -> Text
"雲林縣"
  Letter
Q -> Text
"嘉義縣"
  Letter
R -> Text
"臺南縣"
  Letter
S -> Text
"高雄縣"
  Letter
T -> Text
"屏東縣"
  Letter
U -> Text
"花蓮縣"
  Letter
V -> Text
"臺東縣"
  Letter
W -> Text
"金門縣"
  Letter
X -> Text
"澎湖縣"
  Letter
Y -> Text
"陽明山"
  Letter
Z -> Text
"連江縣"

toTextEnglish :: Location -> Text
toTextEnglish :: Location -> Text
toTextEnglish (Location Letter
letter) = case Letter
letter of
  Letter
A -> Text
"Taipei City"
  Letter
B -> Text
"Taichung City"
  Letter
C -> Text
"Keelung City"
  Letter
D -> Text
"Tainan City"
  Letter
E -> Text
"Kaohsiung City"
  Letter
F -> Text
"New Taipei City"
  Letter
G -> Text
"Yilan County"
  Letter
H -> Text
"Taoyuan City"
  Letter
I -> Text
"Chiayi City"
  Letter
J -> Text
"Hsinchu County"
  Letter
K -> Text
"Miaoli County"
  Letter
L -> Text
"Taichung County"
  Letter
M -> Text
"Nantou County"
  Letter
N -> Text
"Changhua County"
  Letter
O -> Text
"Hsinchu City"
  Letter
P -> Text
"Yunlin County"
  Letter
Q -> Text
"Chiayi County"
  Letter
R -> Text
"Tainan County"
  Letter
S -> Text
"Kaohsiung County"
  Letter
T -> Text
"Pingtung County"
  Letter
U -> Text
"Hualien County"
  Letter
V -> Text
"Taitung County"
  Letter
W -> Text
"Kinmen County"
  Letter
X -> Text
"Penghu County"
  Letter
Y -> Text
"Yangmingshan"
  Letter
Z -> Text
"Lienchiang County"

-- | Generates a random 'Location'.
--
generate :: MonadRandom m => m Location
generate :: forall (m :: * -> *). MonadRandom m => m Location
generate = m Location
forall (m :: * -> *) a.
(MonadRandom m, Finitary a, 1 <= Cardinality a) =>
m a
randomFinitary