{-# LANGUAGE OverloadedStrings #-}

module Text.Hastily.Types (
        MovieInfo(MovieInfo),
        imdb, year, name, movieType,
        SubtitleDialog(SubtitleDialog),
        start_time, end_time,
        digest, dialog,
        Subtitle (Subtitle),
        subtitle_movieinfo, subtitle_dialogs, subtitle_file
        ) where

import           Data.Aeson
import           Data.String.Conversions
import           Data.Text
import           System.FilePath
import           Text.Printf
import           Data.Functor   

data MovieInfo = MovieInfo {imdb::Text, year::Text, name::Text, movieType::Text}

data SubtitleDialog = SubtitleDialog {start_time::Text, end_time::Text, dialog::Text, digest::Text} deriving (Show)

data Subtitle = Subtitle {subtitle_movieinfo::MovieInfo, subtitle_file::FilePath, subtitle_dialogs::[SubtitleDialog]} deriving (Show)

instance Eq SubtitleDialog where
    (==) a b = digest a == digest b

instance Ord SubtitleDialog where
    compare a b = compare (digest a) (digest b)

instance Show MovieInfo where
    show movie_info = printf "%s , Year: %s, Imdb: %s, Type: %s" movie_name movie_year movie_imdb movie_type
                where
                    movie_name = (cs $ name movie_info)::String
                    movie_imdb = (cs $ imdb movie_info)::String
                    movie_year = (cs $ year movie_info)::String
                    movie_type = (cs $ movieType movie_info)::String

instance FromJSON MovieInfo where
    parseJSON (Object v) = MovieInfo <$> v .: "imdbID" <*> v .: "Year"<*> v .: "Title"<*> v .: "Type"