module Gui where

import Graphics.UI.Gtk
import SimpleListView
import Web
import Cache
import Data.IORef

data GuiData = GuiData [IO CacheEntry] (UpdateTreeView) Image Label Label | EmptyGuiData

guidata_get_cache_list (GuiData cache_list _ _ _ _) = cache_list
guidata_get_update_list (GuiData _ update_list _ _ _) = update_list
guidata_get_image (GuiData _ _ image _ _) = image
guidata_get_title_label (GuiData _ _ _ title _) = title
guidata_get_description_label (GuiData _ _ _ _ description) = description
guidata_ref_update_cache_list state cache_list =  do
  guidata <- readIORef state
  writeIORef state (GuiData cache_list (guidata_get_update_list guidata) (guidata_get_image guidata) (guidata_get_title_label guidata) (guidata_get_description_label guidata))
guidata_ref_update_list state update_list = do
  guidata <- readIORef state
  writeIORef state (GuiData (guidata_get_cache_list guidata) update_list (guidata_get_image guidata) (guidata_get_title_label guidata) (guidata_get_description_label guidata))


selected_item :: IORef GuiData -> String -> IO()
selected_item state s = do
  guidata <- readIORef state
  list <- return (guidata_get_cache_list guidata)
  let id = read s :: Int
  cacheentry <- list !! (id - 1)
  putStrLn $ show (get_cache_entry_id cacheentry)
  imageSetFromFile (guidata_get_image guidata) (cache_entry_file_image_path id)
  let titletext = cache_entry_get_title cacheentry
  let desctext = cache_entry_get_description cacheentry
  let title = guidata_get_title_label guidata
  let desc = guidata_get_description_label guidata
  set_title_label_text title titletext
  labelSetText desc desctext

prepare_comics_buffer_widget :: IORef GuiData -> IO()
prepare_comics_buffer_widget state = do
  guidata <- readIORef state
  UpdateTreeView updatelist <- return $ guidata_get_update_list guidata
  let showids = map (\i -> show i) [1..cache_comics_num]
  new_ul <- updatelist showids (selected_item state)
  guidata_ref_update_list state new_ul


set_title_label_text label text = do
  labelSetMarkup label ("<big><b>" ++ text ++ "</b></big>")


gui :: IO()
gui = do
  guidataref <- newIORef EmptyGuiData
  
  initGUI
  window <- windowNew
  mainhbox <- hBoxNew False 10
  imagevbox <- vBoxNew False 10
  labelsvbox <- vBoxNew False 10
  titlelabel <- labelNew (Just "")
  set_title_label_text titlelabel ("Tu znajduje się tytuł komiksu")
  descriptionlabel <- labelNew (Just "Tu znajduje się opis komiksu")
  imageview <- imageNew
  
  listscrolledwindow <- scrolledWindowNew Nothing Nothing
  scrolledWindowSetPolicy listscrolledwindow PolicyNever PolicyAutomatic
  (listview, UpdateTreeView updateList) <- simpleListView "Bufor komiksów" [] (selected_item guidataref)
  scrolledWindowAddWithViewport listscrolledwindow listview

  writeIORef guidataref (GuiData init_cache_list (UpdateTreeView updateList) imageview titlelabel descriptionlabel)

  imagescrolledwindow <- scrolledWindowNew Nothing Nothing
  scrolledWindowSetPolicy imagescrolledwindow PolicyAutomatic PolicyAutomatic
  scrolledWindowAddWithViewport imagescrolledwindow imageview

  set window [containerBorderWidth := 10, containerChild := mainhbox]

  -- list
  boxPackStart mainhbox listscrolledwindow PackNatural 0

  -- image box
  boxPackStart mainhbox imagevbox PackGrow 0

  -- labels
  boxPackStart imagevbox labelsvbox PackNatural 0

  boxPackStart labelsvbox titlelabel PackNatural 0
  boxPackStart labelsvbox descriptionlabel PackNatural 0
  
  -- image
  boxPackStart imagevbox imagescrolledwindow PackGrow 0

  onDestroy window mainQuit
  widgetShowAll window

  prepare_comics_buffer_widget guidataref
  
  mainGUI
