module View.Timer ( timerView ) where import Html (..) import Html.Attributes (..) import Html.Events (..) import String import Time (Time) import Signal import Maybe import List import Model.Model (..) import Model.Timer (..) import Model.TimerEdition (..) import Model.TimerState (..) import Model.Id (..) import Update.Update (..) import Update.UpdateTimer (..) import View.ActivatedClasses (..) import Utils.Maybe (..) timerView : Model -> (Id, Timer) -> Html timerView model (id, timer) = div [ class "timer" ] [ nameBlock (id, timer) , timeBlock model (id, timer) , playPauseBlock (id, timer) , stopBlock (id, timer) , removeBlock (id, timer) ] nameBlock : (Id, Timer) -> Html nameBlock (id, timer) = button [ class "name block" ] [ text timer.name ] timeBlock : Model -> (Id, Timer) -> Html timeBlock model (id, timer) = let maybeEdition = filterMaybe (\te -> te.id == id) model.timerEdition in case maybeEdition of Just edition -> let isEmptyEdition = List.isEmpty edition.numbers in button [ [ (True, "time block edition") , (isEmptyEdition, "empty") ] |> activatedClasses , onClick (Signal.send updates ValidTimerEdition) ] [ if isEmptyEdition then timeWithProgressBar timer else text (editionView edition.numbers) ] Nothing -> button [ [ (True, "time block") , (timer.state == Ringing, "isRinging") , (timer.state == Running, "isRunning") ] |> activatedClasses , onClick <| if timer.state == Ringing then Signal.send updates (UpdateTimer id Stop) else Signal.send updates (EditTimer id) ] [ timeWithProgressBar timer ] editionView : Numbers -> String editionView numbers = let (minutes, seconds) = toMinutesAndSeconds numbers in minutes ++ " : " ++ seconds timeView : Time -> String timeView time = let totalSeconds = truncate (time / 1000) totalMinutes = totalSeconds // 60 restSeconds = totalSeconds `rem` 60 in (String.padLeft 2 '0' (toString totalMinutes)) ++ " : " ++ (String.padLeft 2 '0' (toString restSeconds)) timeWithProgressBar : Timer -> Html timeWithProgressBar timer = div [] [ span [ class "progressBar" , let width = 1 - timer.currentTime / (initTime timer.initialTime) |> (*) 198 |> round |> toString |> flip String.append "px" in style [ ("width", width) ] ] [] , span [ class "text" ] [ text (timeView timer.currentTime) ] ] playPauseBlock : (Id, Timer) -> Html playPauseBlock (id, timer) = button [ class <| "playPause block" , onClick (Signal.send updates (UpdateTimer id ToggleRunning)) ] [ let icon = if timer.state == Running then "fa-pause" else "fa-play" in i [ class <| "fa fa-fw " ++ icon ] [] ] stopBlock : (Id, Timer) -> Html stopBlock (id, timer) = button [ class <| "stop block" , onClick (Signal.send updates (UpdateTimer id Stop)) ] [ i [ class "fa fa-fw fa-stop" ] [] ] removeBlock : (Id, Timer) -> Html removeBlock (id, timer) = button [ class <| "remove block" , onClick (Signal.send updates (RemoveTimer id)) ] [ i [ class "fa fa-fw fa-remove" ] [] ]