<commit_before>module Page.Project exposing (..)

import Page.Errored as Errored exposing (PageLoadError, pageLoadError)
import Request.Project
import Task exposing (Task)
import Data.Session as Session exposing (Session)
import Data.Project as Project exposing (Project)
import Data.Branch as Branch exposing (Branch)
import Html exposing (..)
import Html.Attributes exposing (..)
import Views.Page as Page
import Util exposing ((=>), viewIf)
import Http
import Route exposing (Route)
import Views.Page as Page exposing (ActivePage)
import Page.Project.Route as ProjectRoute
import Page.Project.Commits as Commits
import Page.Project.Settings as Settings
import Page.Project.Commit as Commit
import Page.Project.Overview as Overview
import Views.Helpers exposing (onClickPage)
import Navigation exposing (newUrl)
import Socket.Channel as Channel exposing (Channel)
import Socket.Socket as Socket exposing (Socket)
import Data.PaginatedList as PaginatedList exposing (Paginated(..))
import Json.Encode as Encode


-- SUB PAGES --


type SubPage
    = Blank
    | Overview
    | Commits Commits.Model
    | Commit Commit.Model
    | Settings Settings.Model
    | Errored PageLoadError


type SubPageState
    = Loaded SubPage
    | TransitioningFrom SubPage



-- MODEL --


type alias Model =
    { project : Project
    , branches : List Branch
    , subPageState : SubPageState
    }


initialSubPage : SubPage
initialSubPage =
    Blank


init : Session msg -> Project.Id -> Maybe ProjectRoute.Route -> Task PageLoadError ( Model, Cmd Msg )
init session id maybeRoute =
    let
        maybeAuthToken =
            Maybe.map .token session.user

        loadProject =
            maybeAuthToken
                |> Request.Project.get id
                |> Http.toTask

        loadBranches =
            maybeAuthToken
                |> Request.Project.branches id
                |> Http.toTask

        initialModel project (Paginated branches) =
            { project = project
            , branches = branches.results
            , subPageState = Loaded initialSubPage
            }

        handleLoadError e =
            case e of
                Http.BadPayload debugError _ ->
                    pageLoadError Page.Project debugError

                _ ->
                    pageLoadError Page.Project "Project unavailable."
    in
        Task.map2 initialModel loadProject loadBranches
            |> Task.andThen
                (\successModel ->
                    case maybeRoute of
                        Just route ->
                            update session (SetRoute maybeRoute) successModel
                                |> Task.succeed

                        Nothing ->
                            ( successModel, Cmd.none )
                                |> Task.succeed
                )
            |> Task.mapError handleLoadError



-- CHANNELS --


channelName : Project.Id -> String
channelName projectId =
    (Project.idToString projectId) ++ "projects:"


events : ProjectRoute.Route -> List ( String, Encode.Value -> Msg )
events route =
    case route of
        ProjectRoute.Commits _ _ ->
            List.map (Tuple.mapSecond (\msg -> msg >> CommitsMsg)) Commits.events

        _ ->
            []



-- VIEW --


type ActiveSubPage
    = OtherPage
    | OverviewPage
    | CommitsPage
    | SettingsPage


view : Session msg -> Model -> Html Msg
view session model =
    let
        ( subPageFrame, breadcrumb ) =<commit_msg>[web] fixed project channel name<commit_after>module Page.Project exposing (..)

import Page.Errored as Errored exposing (PageLoadError, pageLoadError)
import Request.Project
import Task exposing (Task)
import Data.Session as Session exposing (Session)
import Data.Project as Project exposing (Project)
import Data.Branch as Branch exposing (Branch)
import Html exposing (..)
import Html.Attributes exposing (..)
import Views.Page as Page
import Util exposing ((=>), viewIf)
import Http
import Route exposing (Route)
import Views.Page as Page exposing (ActivePage)
import Page.Project.Route as ProjectRoute
import Page.Project.Commits as Commits
import Page.Project.Settings as Settings
import Page.Project.Commit as Commit
import Page.Project.Overview as Overview
import Views.Helpers exposing (onClickPage)
import Navigation exposing (newUrl)
import Socket.Channel as Channel exposing (Channel)
import Socket.Socket as Socket exposing (Socket)
import Data.PaginatedList as PaginatedList exposing (Paginated(..))
import Json.Encode as Encode


-- SUB PAGES --


type SubPage
    = Blank
    | Overview
    | Commits Commits.Model
    | Commit Commit.Model
    | Settings Settings.Model
    | Errored PageLoadError


type SubPageState
    = Loaded SubPage
    | TransitioningFrom SubPage



-- MODEL --


type alias Model =
    { project : Project
    , branches : List Branch
    , subPageState : SubPageState
    }


initialSubPage : SubPage
initialSubPage =
    Blank


init : Session msg -> Project.Id -> Maybe ProjectRoute.Route -> Task PageLoadError ( Model, Cmd Msg )
init session id maybeRoute =
    let
        maybeAuthToken =
            Maybe.map .token session.user

        loadProject =
            maybeAuthToken
                |> Request.Project.get id
                |> Http.toTask

        loadBranches =
            maybeAuthToken
                |> Request.Project.branches id
                |> Http.toTask

        initialModel project (Paginated branches) =
            { project = project
            , branches = branches.results
            , subPageState = Loaded initialSubPage
            }

        handleLoadError e =
            case e of
                Http.BadPayload debugError _ ->
                    pageLoadError Page.Project debugError

                _ ->
                    pageLoadError Page.Project "Project unavailable."
    in
        Task.map2 initialModel loadProject loadBranches
            |> Task.andThen
                (\successModel ->
                    case maybeRoute of
                        Just route ->
                            update session (SetRoute maybeRoute) successModel
                                |> Task.succeed

                        Nothing ->
                            ( successModel, Cmd.none )
                                |> Task.succeed
                )
            |> Task.mapError handleLoadError



-- CHANNELS --


channelName : Project.Id -> String
channelName projectId =
    "projects:" ++ (Project.idToString projectId)


events : ProjectRoute.Route -> List ( String, Encode.Value -> Msg )
events route =
    case route of
        ProjectRoute.Commits _ _ ->
            List.map (Tuple.mapSecond (\msg -> msg >> CommitsMsg)) Commits.events

        _ ->
            []



-- VIEW --


type ActiveSubPage
    = OtherPage
    | OverviewPage
    | CommitsPage
    | SettingsPage


view : Session msg -> Model -> Html Msg
view session model =
    let
        ( subPageFrame, breadcrumb ) =
{'fraction_non_alphanumeric': 0.07254480286738352, 'fraction_numerical': 0.0002867383512544803, 'mean_word_length': 1.9978513107004727, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6121170', 'n_tokens_mistral': 1873, 'n_tokens_neox': 1721, 'n_words': 544}