#include "../avatarui_common.lua"
#include "script/player_characters.lua"

local bindChar = Ui.bind().viewModel.charModel

Avatar.CharItemView = function(obj)
    local item = Ui.Frame
    {
        w = 96 * Ui.bind().viewModel.getScale,
        h = 97 * Ui.bind().viewModel.getScale,

        Ui.Rect
        {
            resizeToParent = true,

            vAlign = Ui.ContentLayout.Vertical.Bottom,
            hAlign = Ui.ContentLayout.Horizontal.Center,

            id = "preview",

            cornerRadius = 9,
            fillImagePath = bindChar.isLocked:band(bindChar.lockedPreviewPath):bor(bindChar.previewPath)
        },

        Ui.Image
        {
            id = "tickMark",

            w = 28 * Ui.bind().viewModel.getScale,
            h = 26 * Ui.bind().viewModel.getScale,

            vAlign = Ui.V.Top,
            hAlign = Ui.H.Right,

            path = "avatarui/resources/tickmark.png",
            visible = bindChar
                :call(
                    function(_, char)
                        if #char.skins > 1 then
                            return tableAny(char.skins, function(s) return s.isSelected end)
                        else
                            return char.isSelected
                        end
                    end)
        },

        Ui.RectOutline
        {
            id = "highlight",
            resizeToParent = true,
            cornerRadius = 9,
            thickness = 2,
            visible = Ui.bind():call(function () return GetScreenSize().h > 768 end),
            color = bindChar.isFocused:band(0xFFFF7F):bor(0x8E7F48)
        },

        Ui.Image
        {
            w = 30,
            h = 30,
            margin = { bottom = 3, right = 3 },
            vAlign = Ui.ContentLayout.Vertical.Bottom,
            hAlign = Ui.ContentLayout.Horizontal.Right,
            path = "avatarui/resources/modded_char_icon.png",
            visible = bindChar:call(function(_, char) 
                return char.group == "modded" and 
                    char.previewPath ~= gPlayerCharacters["modded"].preview 
                end)
        },

        Ui.Frame
        {
            w = 20,
            h = 20,
            margin = { top = -10, right = -10 },

            vAlign = Ui.V.Top,
            hAlign = Ui.H.Right,

            visible = bindChar.isFresh,

            Ui.Circle
            {
                vAlign = Ui.V.Middle,
                hAlign = Ui.H.Center,
                radius = 10,
                color = 0xD72626
            },

            Ui.CircleOutline
            {
                vAlign = Ui.V.Middle,
                hAlign = Ui.H.Center,
                radius = 10,
                color = { 1, 1, 1 },
                thickness = 1
            }
        }
    }

    return item:new(obj)
end
