import System.IO
import System.Environment
import Data.Maybe
import Control.Exception
import Database.HDBC
import Database.HDBC.PostgreSQL

-- ------------------------------

pqGetValue :: [[SqlValue]] -> Int -> Int -> String
pqGetValue xs tuple_num field_num = fromSql $ (xs !! tuple_num) !! field_num

-- ------------------------------

form_table_cell :: [[SqlValue]] -> Int -> Int -> IO ()
form_table_cell result i j = do
 putStr $ "<td valign=center>" ++ pqGetValue result i j ++ "</td>"

-- ...............................

form_table_row :: [[SqlValue]] -> Int -> IO ()
form_table_row result i = do
 putStr "<tr>"
 mapM (form_table_cell result i) [0..(length (result!!0))-1]
 putStr "</tr>"

-- ................................

form_table :: [[SqlValue]] -> IO ()
form_table result =do
 putStr "<table border>"
 mapM (form_table_row result) [0..(length (result))-1]
 putStr "</table>"

-- ==============================

main = 
 Control.Exception.catch (do
  e <- getEnv "WWW_symptom1" -- first symptom field should not be empty!
  f' <- lookupEnv "WWW_symptom2"
  let f = fromMaybe " " f'
  g' <- lookupEnv "WWW_symptom3"
  let g = fromMaybe " " g'
  h' <- lookupEnv "WWW_symptom4"
  let h = fromMaybe " " h'

  let lev4 = e++" "++f++" "++g++" "++h

  passString <- getEnv "HOMOEOPIM_POOKAI_PASSWORD"
  let connString = "host=localhost port=5433 dbname=homoeopim user=pookai password=" ++ passString
  c <- connectPostgreSQL connString

  let query1 = "select symptom,concat('<button type=button class=bhoot onclick=my_load(',id,')>',id,'</button>') from symptom where clips_lhs like '%"++e++"%' and clips_lhs like '%"++f++"%' and clips_lhs like '%"++g++"%' and clips_lhs like '%"++h++"%' order by similarity(clips_lhs,'"++lev4++"') desc limit 10"
  moja1 <- prepare c query1

  let query2 = "select symptom,concat('<button type=button class=bhoot onclick=my_load(',id,')>',id,'</button>') from symptom where (clips_lhs like '%"++e++"%' and clips_lhs like '%"++f++"%' and clips_lhs like '%"++g++"%') or (clips_lhs like '%"++e++"%' and clips_lhs like '%"++f++"%' and clips_lhs like '%"++h++"%') or (clips_lhs like '%"++e++"%' and clips_lhs like '%"++g++"%' and clips_lhs like '%"++h++"%') order by similarity(clips_lhs,'"++lev4++"') desc limit 10"
  moja2 <- prepare c query2

  let query3 = "select symptom,concat('<button type=button class=bhoot onclick=my_load(',id,')>',id,'</button>') from symptom where (clips_lhs like '%"++e++"%' and clips_lhs like '%"++f++"%') or (clips_lhs like '%"++e++"%' and clips_lhs like '%"++g++"%') or (clips_lhs like '%"++e++"%' and clips_lhs like '%"++h++"%') order by similarity(clips_lhs,'"++lev4++"') desc limit 20"
  moja3 <- prepare c query3

  let query4 = "select symptom,concat('<button type=button class=bhoot onclick=my_load(',id,')>',id,'</button>') from symptom where clips_lhs like '%"++e++"%' or clips_lhs like '%"++f++"%' or clips_lhs like '%"++g++"%' or clips_lhs like '%"++h++"%' order by similarity(clips_lhs,'"++lev4++"') desc limit 30"
  moja4 <- prepare c query4

  execute moja1 []
  result1 <- fetchAllRows moja1
  execute moja2 []
  result2 <- fetchAllRows moja2
  execute moja3 []
  result3 <- fetchAllRows moja3
  execute moja4 []
  result4 <- fetchAllRows moja4

  putStrLn "Content-type: text/plain\n"

  putStr $ "{\"moja1\":\""
  form_table result1
  putStr "\","

  putStr $ "\"moja2\":\""
  form_table result2
  putStr "\","

  putStr $ "\"moja3\":\""
  form_table result3
  putStr "\","

  putStr $ "\"moja4\":\""
  form_table result4

  putStr "\"}"

  disconnect c)
  handler where
   handler :: SqlError -> IO ()
   handler err = putStrLn $ "Content-type: text/plain\n\nOh no: " ++ show err

