module ChapterEditApp.Models exposing (..)

import Json.Encode
import Json.Decode
import Common.Models exposing (FullCharacter, Character, Narration, Chapter, Banner)


newEmptyChapter : Narration -> Chapter
newEmptyChapter narration =
    { id = 0
    , narrationId = narration.id
    , title = ""
    , audio = narration.defaultAudio
    , backgroundImage = narration.defaultBackgroundImage
    , text = Json.Encode.list []
    , participants = narration.characters
    , published = Nothing
    }


type alias LastReactionChapter =
    { id : Int
    , title : String
    }


type alias LastChapter =
    { id : Int
    , title : String
    , text : Json.Decode.Value
    }


type alias LastReaction =
    { chapterInfo : LastReactionChapter
    , character : Character
    , text : Maybe String
    }


type alias LastReactions =
    { chapterId : Int
    , reactions : List LastReaction
    , chapters : List LastChapter
    }


type alias Model =
    { chapter : Maybe Chapter
    , narration : Maybe Narration
    , lastReactions : Maybe LastReactions
    , banner : Maybe Banner
    , flash : Maybe Banner
    , showPublishChapterDialog : Bool
    , uploadingAudio : Bool
    , uploadingBackgroundImage : Bool
    }

module ChapterEditApp.Models exposing (..)

import Json.Encode
import Json.Decode
import Common.Models exposing (FullCharacter, Character, Narration, Chapter, Banner)


newEmptyChapter : Narration -> Chapter
newEmptyChapter narration =
    { id = 0
    , narrationId = narration.id
    , title = ""
    , audio = narration.defaultAudio
    , backgroundImage = narration.defaultBackgroundImage
    , text = Json.Encode.object [ ("type", Json.Encode.string "doc")
                                , ("content", Json.Encode.list [])
                                ]
    , participants = narration.characters
    , published = Nothing
    }


type alias LastReactionChapter =
    { id : Int
    , title : String
    }


type alias LastChapter =
    { id : Int
    , title : String
    , text : Json.Decode.Value
    }


type alias LastReaction =
    { chapterInfo : LastReactionChapter
    , character : Character
    , text : Maybe String
    }


type alias LastReactions =
    { chapterId : Int
    , reactions : List LastReaction
    , chapters : List LastChapter
    }


type alias Model =
    { chapter : Maybe Chapter
    , narration : Maybe Narration
    , lastReactions : Maybe LastReactions
    , banner : Maybe Banner
    , flash : Maybe Banner
    , showPublishChapterDialog : Bool
    , uploadingAudio : Bool
    , uploadingBackgroundImage : Bool
    }

Fix structure of empty chapters
Fix structure of empty chapters

When creating a new chapter, if the Save or Publish buttons are
pushed without writing anything in the box, the saved chapter
will have an invalid structure (it will be an empty array,
instead of an object with the "type" and "content" properties).
To fix the issue: define the default, empty chapter correctly.
