import Text.Read
import Data.Maybe
import Sound.Tidal.Chords
import TemperatureData
import HumidityData
import BatteryData
import Graphics.Gnuplot.Simple
import qualified Graphics.Gnuplot.Terminal.QT as QT
import System.Random


d1 $ s "[bd*4,  ~!2 hh ~ hh*3 hh ~]" # room 0.1

randInt :: Int -> Int -> IO Int
randInt minVal maxVal = randomRIO (minVal, maxVal)

getRawValueList :: [(a, a)] -> [a]
getRawValueList tableData = map (\x -> snd x) tableData

getRawLabelList :: [(b, b)] -> [b]
getRawLabelList tableData = map (\x -> fst x) tableData

makePlottableList :: [c] -> [(Int, c)]
makePlottableList list = zip [0..] list

plotPlainList :: [(Int, d)] -> IO ()
plotPlainList list = plotList [] $ makePlottableList list

listToNum :: [String] -> [Float]
listToNum list = map (\x -> getNum x 1) list

getRandomStartIndex :: Int -> Int -> IO Int
getRandomStartIndex listLength sliceSize = do
  gen <- getStdGen
  let validRange = listLength - sliceSize + 1
  randomInt <- randomR (0, validRange - 1) gen
  return randomInt

randomSlice :: Int -> [a] -> [a]
randomSlice list length = take 


getNum :: String -> Float -> Float
getNum value scalar = fromInteger $ round $ (maybe 0.0 id (readMaybe value :: Maybe Float)) * scalar
sliceList start end list  = take (end - start + 1) (drop start list)

normalizeBetween :: Int Int [Float] -> [Float]
normalizeBetween min max list = map (\x -> (max - min) * ((x - min)/ (maximum (map (\y -> y - min) list)))) list

normalizeAt :: Int Int [Float] -> [Float]
normalizeAt zero max list = map (\x -> max * ((x - zero)/ (maximum (map (\y -> y - zero) list)))) list



map (\x -> max * ( x / (maximum list))) list


maximum [1,2,3,4,5]


let temperatureScalar = 10
    temperaturePattern = scale "major" (fromList (map (\x -> getNum x temperatureScalar)  (map (\x -> snd x)  (take 10 temperaturedata))) |- 150)
    temperature = fast 4 $ n ("x(3,5)" |> temperaturePattern + "['maj | 'min]" )# s "superpiano" # room 0.5 
    in d1 $ temperature

queryArc (n ("1 2 3 4 5 6 7 8")) (Arc 0 1)

:i fromInteger

:t temperaturedata

length $ listToNum $ getRawValueList $ temperaturedata

plotList [] (zip [0..] (listToNum (getRawValueList $ take 10 temperaturedata)))

:t temperaturedata


selection = listToNum (getRawValueList $ take 10 temperaturedata)

plotPlainList (selection)


let batteryScalar = 5
    batteryPattern = scale "major" (fromList (map (\x -> getNum x batteryScalar)  (map (\x -> snd x)  (sliceList 500 510 batterydata))) |- 150)
    battery = n ("x(3,5)" |> batteryPattern + "['maj | 'min]" )# s "jvbass" # room 0.5 
    in d2 $ battery

battery = n ("x(3,5)" |> batteryPattern + "['maj | 'min]" )
batteryScalar = 5
queryArc battery (Arc 0 2)

queryArc temperature (Arc 0 2.5)

d1 $ fast 10 $ append (n (fromList (map (\x -> fromInteger (round ((getFloat x)*10)))  (map (\x -> snd x)  (take 100 temperaturedata))))) ("~!10") # s "jvbass" # room 0.5 # delay 0.7

d2 $ fast 10 $ n (fromList (map (\x -> fromInteger (round ((getFloat x)*5)))  (map (\x -> snd x)  (take 100 humiditydata)))) # s "arpy" # room 0.5 # delay 0.7

d1 $ trigger $ s "<bd sn*4 bd sn*2>"

d1 $ n ( run 10) # s "v"
:t map (\x -> getMaybeFloat (snd x))  (take 100 temperaturedata)

d1 $ n("c'maj f4'maj'ii g4'maj'i c'maj") # s "superpiano"

d1 $ slow 4 $ n "[1..5]" # s "meteo"