module Deck exposing (Deck, cmcCurve, fromDraft)

import Card exposing (CardData)
import Database exposing (Database)
import Draft exposing (Draft)
import List.Extra as List exposing (group)
import Zipper


type alias Deck =
    { cards : List CardData }


fromDraft : Bool -> Database -> Draft -> Deck
fromDraft includeWholeDeck db draft =
    let
        picks =
            if includeWholeDeck then
                Zipper.toList draft |> List.map .chosen

            else
                (Zipper.focus draft :: Zipper.left draft) |> List.map .chosen

        cards =
            List.filterMap (\pick -> Database.get pick.name db) picks
    in
    { cards = cards }


cmcCurve : Deck -> List ( Int, Int )
cmcCurve deck =
    let
        maxBucket =
            7

        cmcs =
            deck.cards
                |> List.map (.details >> .cmc)
    in
    (List.range 0 maxBucket
        |> List.map (\cmc -> ( cmc, List.count (\x -> x == cmc) cmcs ))
    )
        ++ [ ( maxBucket, List.count (\x -> x >= maxBucket) cmcs ) ]
