<commit_before>module Board
    exposing
        ( init
        , view
        , update
        , subscriptions
        )

-- import Effects exposing (Effects)

import Html exposing (Html)
import Html.App


-- import Html.Attributes exposing (..)

import Svg exposing (..)
import Svg.Attributes exposing (..)


-- import Html.Events exposing (onClick)

import Matrix exposing (Matrix)
import Position
import Piece
import Chain
import Maybe exposing (..)
import Color exposing (Color, lightBrown, darkBrown)
import Time exposing (Time, second)
import Window
import Keyboard exposing (KeyCode)
import Debug exposing (log)


-- MODEL


type alias PosCount =
    Int


maxPosLength : PosCount
maxPosLength =
    11


type alias BoardSideInPixels =
    Int


boardSideInPixels : BoardSideInPixels
boardSideInPixels =
    400


sideSize =
    (toFloat boardSideInPixels)
        / (toFloat maxPosLength)


squareType : Matrix.Location -> Position.PositionType
squareType location =
    let
        ( x, y ) =
            location

        maxPos =
            maxPosLength - 1
    in
        if
            (x == 0)
                || (x == maxPos)
                || (y == 0)
                || (y == maxPos)
        then
            Position.Perimeter
        else
            Position.Grid


positionFromInit : Matrix.Location -> Position.Model
positionFromInit location =
    let
        ( position, msg ) =
            Position.initWithInfo (squareType location)
                maxPosLength
                sideSize
                location
    in
        position


createMatrix : PosCount -> Matrix Position.Model
createMatrix posCount =
    Matrix.square posCount
        (\location -> positionFromInit location)


type alias Model =
    { board : Matrix Position.Model
    , pieces : List Piece.Model
    , chain : Chain.Model
    }


type alias PositionLocator =
    { location : Matrix.Location
    , model : Position.Model
    }


piecesInfo =
    [ ( 47, 2, 2 )
    , ( 48, 3, 2 )
    , ( 49, 3, 3 )
--    , ( 4, 8, 6 )
--    , ( 5, 9, 9 )
    ]


initPiece : ( Int, Int, Int ) -> Piece.Model
initPiece tuple =
    let
        ( pieceNumber, x, y ) =
            tuple

        ( piece, _ ) =
            Piece.initWithInfo pieceNumber
                sideSize
                ( x, y )
    in
        piece


initChain : List Piece.Model -> Chain.Model
initChain pieces =
    List.take 3 pieces


init : ( Model, Cmd Msg )
init =
    let
        board =
            createMatrix maxPosLength

        pieces =
            List.map (\pieceInfo -> (initPiece pieceInfo))
                piecesInfo

        chain =
            initChain pieces
    in
        ( { board = board
          , pieces = pieces
          , chain = chain
          }
        , Cmd.none
        )



-- UPDATE


type Msg
    = ModifyPosition Matrix.Location Position.Msg
    | ModifyPiece Matrix.Location Piece.Msg
    | KeyDown KeyCode


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        ModifyPosition location positionMsg ->
            ( model, Cmd.none )

        ModifyPiece location pieceMsg ->
            ( model, Cmd.none )

        KeyDown keyCode ->
            let
                chainMsg =
                    Chain.KeyDown keyCode

                logChain =
                    model.chain

--                logHeadPiece =
--                    log "head" (List.head logChain)

                ( chain, _ ) =
                    Chain.update chainMsg model.chain

                updatedModel =
                    { model | chain = chain }
            in
                ( updatedModel, Cmd.none )



-- SUBSCRIPTIONS


subscriptions : Model -> Sub Msg
subscriptions model =
    Sub.batch
        [ Keyboard.downs KeyDown
        ]



-- VIEW
-- Number of positions on the side of the boafd


type alias Width =
    Int

<commit_msg>Now have 81 pieces in one chain<commit_after>module Board
    exposing
        ( init
        , view
        , update
        , subscriptions
        )

-- import Effects exposing (Effects)

import Html exposing (Html)
import Html.App


-- import Html.Attributes exposing (..)

import Svg exposing (..)
import Svg.Attributes exposing (..)


-- import Html.Events exposing (onClick)

import Matrix exposing (Matrix)
import Position
import Piece
import Chain
import Maybe exposing (..)
import Color exposing (Color, lightBrown, darkBrown)
import Time exposing (Time, second)
import Window
import Keyboard exposing (KeyCode)
import Debug exposing (log)


-- MODEL


type alias PosCount =
    Int


maxPosLength : PosCount
maxPosLength =
    11


type alias BoardSideInPixels =
    Int


boardSideInPixels : BoardSideInPixels
boardSideInPixels =
    400


sideSize =
    (toFloat boardSideInPixels)
        / (toFloat maxPosLength)


squareType : Matrix.Location -> Position.PositionType
squareType location =
    let
        ( x, y ) =
            location

        maxPos =
            maxPosLength - 1
    in
        if
            (x == 0)
                || (x == maxPos)
                || (y == 0)
                || (y == maxPos)
        then
            Position.Perimeter
        else
            Position.Grid


positionFromInit : Matrix.Location -> Position.Model
positionFromInit location =
    let
        ( position, msg ) =
            Position.initWithInfo (squareType location)
                maxPosLength
                sideSize
                location
    in
        position


createMatrix : PosCount -> Matrix Position.Model
createMatrix posCount =
    Matrix.square posCount
        (\location -> positionFromInit location)


type alias Model =
    { board : Matrix Position.Model
    , pieces : List Piece.Model
    , chain : Chain.Model
    }


type alias PositionLocator =
    { location : Matrix.Location
    , model : Position.Model
    }


piecesInfo =
    [ ( 47, 2, 2 )
    , ( 48, 3, 2 )
    , ( 49, 3, 3 )
      --    , ( 4, 8, 6 )
      --    , ( 5, 9, 9 )
    ]


create81Pieces : List Piece.Model
create81Pieces =
    List.map (\pos -> createPieceForPos pos) [0..80]


createPieceForPos : Int -> Piece.Model
createPieceForPos position =
    let
        x =
            xForPos position

        y =
            yForPos position

        tuple =
            ( position + 1, x, y )
    in
        initPiece tuple


xForPos : Int -> Int
xForPos position =
    let
        roundTrip =
            position % 18

        leftToRight =
            position % 9

        rightToLeft =
            8 - (position % 9)

        twoRows =
            roundTrip // 9
    in
        case twoRows of
            0 ->
                1 + leftToRight

            1 ->
                1 + rightToLeft

            _ ->
                1


yForPos : Int -> Int
yForPos position =
    1 + (position // 9)


initPiece : ( Int, Int, Int ) -> Piece.Model
initPiece tuple =
    let
        ( pieceNumber, x, y ) =
            tuple

        ( piece, _ ) =
            Piece.initWithInfo pieceNumber
                sideSize
                ( x, y )
    in
        piece


initChain : List Piece.Model -> Chain.Model
initChain pieces =
    List.take 3 pieces


init : ( Model, Cmd Msg )
init =
    let
        board =
            createMatrix maxPosLength

        pieces =
            -- List.map (\pieceInfo -> (initPiece pieceInfo))
            --    piecesInfo
            create81Pieces

        chain =
            --initChain pieces
            pieces
    in
        ( { board = board
          , pieces = pieces
          , chain = chain
          }
        , Cmd.none
        )



-- UPDATE


type Msg
    = ModifyPosition Matrix.Location Position.Msg
    | ModifyPiece Matrix.Location Piece.Msg
    | KeyDown KeyCode


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        ModifyPosition location positionMsg ->
            ( model, Cmd.none )

        ModifyPiece location pieceMsg ->
            ( model, Cmd.none )

        KeyDown keyCode ->
            let
                chainMsg =
                    Chain.KeyDown keyCode

                logChain =
                    model.chain

                --                logHeadPiece =
                --                    log "head" (List.head logChain)
                ( chain, _ ) =
                    Chain.update chainMsg model.chain

                updatedModel =
                    { model | chain = chain }
            in
                ( updatedModel, Cmd.none )



-- SUBSCRIPTIONS


subscriptions : Model -> Sub Msg
subscriptions model =
    Sub.batch
        [ Keyboard.downs KeyDown
        ]



-- VIEW
-- Number of positions on the side of the boafd


type alias Width =
    Int


{'fraction_non_alphanumeric': 0.07868663594470046, 'fraction_numerical': 0.009101382488479262, 'mean_word_length': 1.4522598870056498, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1229925', 'n_tokens_mistral': 2605, 'n_tokens_neox': 2374, 'n_words': 693}