{-|
  Module     : Types
  Copyright  : Copyright (C) 2010 https://github.com/berufsnerd
  License    : GPLv3

  Portability: non-portable
  
  Types for the project, build upon the data module.
  This module provides type conversion from and to string.
-}
module Types where

import Text.ParserCombinators.Parsec hiding (State)

import Parser
import Data

--------------------------------------------------------------------------------
instance Show Reply where
  show Okay         = "O"
  show (Result val) = "R " ++ show val
  show (Error msg)  = "E " ++ show msg
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
instance Show ErrorReason where
  show PermissionError  = "Insufficied rights"
  show (MathError e)    = "Mathematical error: " ++ e
  show (GeneralError e) = "General error: " ++ e
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
instance Show Query where
  show (Auth u p) = "Q AUTH " ++ u ++ " " ++ p
  show (Calc f)   = "Q CALC " ++ show f
  show (Quit)     = "Q QUIT"
  show (Unknown)  = "Q UNKNOWN"

instance Read Query where
  readsPrec _ = either
                  (const [(Unknown,"")])
                  id . parse parsecRead' ""
                where
                  parsecRead' = do
                    a <- readQuery
                    rest <- getInput
                    return [(a, rest)]
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
instance Show Formula where
  show (Addition a b)       = showF "+" a b
  show (Difference a b)     = showF "-" a b
  show (Multiplication a b) = showF "*" a b
  show (Division a b)       = showF "/" a b

-- | Helper Function for "Show Formular"
showF op a b = show a ++ " " ++ op ++ " " ++ show b
instance Read Formula where
  readsPrec _ = either
                  (const [])
                  id . parse parsecRead' ""
                where
                  parsecRead' = do
                    a <- readFormula
                    rest <- getInput
                    return [(a, rest)]
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
instance Show FormulaValue where
  show (SimpleValue a) = show a
  show (LastResult)    = "LastResult"
--------------------------------------------------------------------------------
