module Chest.Selection exposing (Model, Msg, init, modifiers, selected, totalSelectedPrice, update, view) import Api exposing (Item, Loot) import Dict exposing (Dict) import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) import Set exposing (Set) import Table type alias Selection = Set Int type Data a = NoData | Data (Dict Int a) type Model = Model Selection (Data Int) init : Maybe (List Int) -> Bool -> Model init maybeInitial hasData = Model (case maybeInitial of Just initial -> Set.fromList initial Nothing -> Set.empty ) (case hasData of True -> Data Dict.empty False -> NoData ) view : Model -> Loot -> Html Msg view (Model selection data) loot = let isSelected = itemInSelection selection renderRight item = case data of Data inner -> let maybeMod = Dict.get item.id inner in renderItemWithPrice .base_price isSelected maybeMod item NoData -> [] in Table.view (Table.renderSelectableRow (\item -> [ p [] [ text item.name ] ]) renderRight (\item _ -> SwitchSelectionState item.id) isSelected ) loot renderItemWithPrice toPrice isSelected maybeMod item = [ viewPriceWithModApplied (Maybe.map (\i -> toFloatingMod i) maybeMod) (toFloat item.base_price) , if isSelected item then viewPriceModifier item.id <| case maybeMod of Just mod -> String.fromInt mod Nothing -> "0" else text "" ] toFloatingMod : Int -> Float toFloatingMod percent = (100 + Debug.log "toFloat" (toFloat percent)) / 100 -- Renderers : Item -> Html Msg viewPriceWithModApplied : Maybe Float -> Float -> Html Msg viewPriceWithModApplied maybeMod basePrice = case maybeMod of Just mod -> p [ class "level-item has-text-weight-bold" ] [ (Debug.log "withMod" (String.fromFloat (basePrice * mod)) ++ "po") |> text ] Nothing -> p [ class "level-item" ] [ (String.fromFloat basePrice ++ "po") |> text ] viewPriceModifier : Int -> String -> Html Msg viewPriceModifier id modValue = div [ class "level-item field has-addons" ] [ div [ class "control has-icons-left" ] [ input [ type_ "number" , value modValue , class "input is-small" , size 3 , style "width" "6em" , Html.Attributes.min "-50" , Html.Attributes.max "50" , step "5" , onInput (PriceModifierChanged id) ] [] , span [ class "icon is-left" ] [ i [ class "fas fa-percent" ] [] ] ] ] -- Selection -- Get list of selected items selected : Model -> Loot -> Loot selected (Model selection data) loot = List.filter (itemInSelection selection) loot modifiers : Model -> Loot -> List (Maybe Float) modifiers (Model selection data) items = case data of Data inner -> List.map (\item -> Dict.get item.id inner |> Maybe.map toFloatingMod ) items NoData -> [] totalSelectedPrice : Model -> Loot -> Maybe Float totalSelectedPrice model loot = case selected model loot of [] -> Nothing items -> let (Model selection data) = model modifier item = Maybe.withDefault 1.0 <| Maybe.map toFloatingMod <| case data of Data inner -> Dict.get item.id inner NoData -> Nothing in Just <| List.foldl (+) 0.0 <| List.map (\item -> toFloat item.base_price * modifier item) items itemInSelection : Selection -> Item -> Bool itemInSelection selection item = Set.member item.id selection {- itemInClaims : Claims -> Item -> Bool itemInClaims claims item = List.any (\c -> c.loot_id == item.id) claims -} switchSelectionState : Int -> Selection -> Selection switchSelectionState id selection = case Set.member id selection of True -> Set.remove id selection False -> Set.insert id selection type Msg = SwitchSelectionState Int | PriceModifierChanged Int String update : Msg -> Model -> ( Model, Cmd Msg ) update msg (Model selection data) = case msg of PriceModifierChanged id value -> ( Model selection <| case data of Data inner -> Data (Dict.insert id (case String.toInt value of Just i -> i Nothing -> 0 ) inner ) NoData -> data , Cmd.none ) SwitchSelectionState id -> ( Model (switchSelectionState id selection) data, Cmd.none )