﻿local solidComment = {}

--パレットの開け閉めの状態管理
local openFlag = false
local openAnimationFlag = false
local openAnimationStartTime = 0
local closeAnimationFlag = false
local closeAnimationStartTime = 0

--カラーパレットのオブジェクト名
local colorNameTbl = {
    "Gold",
    "Default",
    "Pink",
    "Studio",
    "Blue",
    "Pastel",
    "White"
}

--パレット関係のオブジェクト
local colorCollider = {}
local colorObj = {}
for i = 1, 7 do
    colorCollider[i] = vci.assets.GetTransform("ColorCollider"..i)
    colorObj[i] = vci.assets.GetTransform(colorNameTbl[i])
end
local paletteButtonCollider = vci.assets.GetTransform("PaletteButtonCollider")
local paletteButton = vci.assets.GetTransform("PaletteButton")
local selectMark = vci.assets.GetTransform("Circle")

--カラーパレット番号
local colorPaletteNum = 2

--パレットアニメーション
local paletteAnime = vci.assets.GetTransform("ButtonRoot").GetAnimation()

--カラーパレットTbl
local colorTbl = {}

--金色のカラーパレット
colorTbl[1] = {}
colorTbl[1][1] = Color.__new(0.75, 0.6, 0.08, 1)
colorTbl[1][2] = Color.__new(0.84, 0.7, 0.25, 1)
colorTbl[1][3] = Color.__new(0.9, 0.8, 0.45, 1)
colorTbl[1][4] = Color.__new(0.95, 0.8, 0.05, 1)
colorTbl[1][5] = Color.__new(0.95, 0.9, 0.65, 1)
--デフォルトのカラーパレット（スクリプト上はTable内を参照しない）
colorTbl[2] = {Color.black}
--ピンクのカラーパレット
colorTbl[3] = {}
colorTbl[3][1] = Color.__new(0.94, 0.27, 0.6, 1)
colorTbl[3][2] = Color.__new(0.95, 0.4, 0.5, 1)
colorTbl[3][3] = Color.__new(1, 0.5, 0.75, 1)
colorTbl[3][4] = Color.__new(1, 0.7, 0.8, 1)
colorTbl[3][5] = Color.__new(0.95, 0.8, 0.9, 1)
colorTbl[3][6] = Color.__new(1, 0.2, 0.5, 1)
colorTbl[3][7] = Color.__new(0.85, 0.35, 0.4, 1)
--スタジオのカラーパレット
colorTbl[4] = {}
colorTbl[4][1] = Color.__new(236,236,236,255)/255  --白色
colorTbl[4][2] = Color.__new(200,199,153,255)/255  --薄黄色
colorTbl[4][3] = Color.__new(234,224,5,255)/255    --黄色
colorTbl[4][4] = Color.__new(236,193,1,255)/255    --濃い黄色
colorTbl[4][5] = Color.__new(153,154,1,255)/255    --黄金色
colorTbl[4][6] = Color.__new(0,200,102,255)/255    --薄緑色
colorTbl[4][7] = Color.__new(2,236,1,255)/255      --緑色
colorTbl[4][8] = Color.__new(1,235,234,255)/255   --水色
colorTbl[4][9] = Color.__new(1,1,235,255)/255      --青色
colorTbl[4][10] = Color.__new(103,51,198,255)/255   --紫色
colorTbl[4][11] = Color.__new(193,1,236,255)/255   --パッションピンク
colorTbl[4][12] = Color.__new(235,132,133,255)/255 --ピンク
colorTbl[4][13] = Color.__new(235,1,2,255)/255     --赤色
colorTbl[4][14] = Color.__new(199,1,52,255)/255     --濃い赤色
colorTbl[4][15] = Color.__new(234,105,0,255)/255   --オレンジ
--ブルーのカラーパレット
colorTbl[5] = {}
colorTbl[5][1] = Color.__new(0.4, 0.8, 1, 1)
colorTbl[5][2] = Color.__new(0.65, 0.8, 1, 1)
colorTbl[5][3] = Color.__new(0.85, 0.9, 0.95, 1)
colorTbl[5][4] = Color.__new(0.2, 0.4, 0.7, 1)
colorTbl[5][5] = Color.__new(0.3, 0.36, 0.65, 1)
colorTbl[5][6] = Color.__new(0, 0.2, 0.6, 1)
colorTbl[5][7] = Color.__new(0, 0.38, 0.7, 1)
--パステルカラーのカラーパレット
colorTbl[6] = {}
colorTbl[6][1] = Color.__new(1,       0.5,    0.5,    1)
colorTbl[6][2] = Color.__new(1,       0.5,    0.75,   1)
colorTbl[6][3] = Color.__new(1,       0.5,    1,      1)
colorTbl[6][4] = Color.__new(0.75,    0.5,    1,      1)
colorTbl[6][5] = Color.__new(0.5,     0.5,    1,      1)
colorTbl[6][6] = Color.__new(0.5,     0.75,   1,      1)
colorTbl[6][7] = Color.__new(0.5,     1,      1,      1)
colorTbl[6][8] = Color.__new(0.5,     1,      0.75,   1)
colorTbl[6][9] = Color.__new(0.5,     1,      0.5,    1)
colorTbl[6][10] = Color.__new(0.75,   1,      0.5,    1)
colorTbl[6][11] = Color.__new(1,      1,      0.5,    1)
colorTbl[6][12] = Color.__new(1,      0.75,   0.5,    1)
--白のカラーパレット
colorTbl[7] = {}
colorTbl[7][1] = Color.white
colorTbl[7][2] = Color.white


--- カラーパレット番号に応じた、マテリアル設定処理
--- @param options table @立体コメントの設定値
--- @return table @マテリアル設定後の立体コメントの設定値
local function colorPatternOptionSetting(options)
    if colorPaletteNum == 2 then
        --デフォルト設定カラーなので、特に処理なし
        return options
    end
    if colorPaletteNum == 1 then
        options.mainMaterial.metallic = 0.8
        options.mainMaterial.smoothness = 0.8
        options.sideMaterial.metallic = 0.8
        options.sideMaterial.smoothness = 0.8
    end
    local randomNum = math.random(#colorTbl[colorPaletteNum])
    if colorPaletteNum == 4 then
        --Unlitライクに見せるマテリアル設定
        options.mainMaterial.color = Color.black
        options.mainMaterial.emissionColor = colorTbl[colorPaletteNum][randomNum].linear
        options.sideMaterial.color = Color.black
        options.sideMaterial.emissionColor = (colorTbl[colorPaletteNum][randomNum]*0.7).linear
    else
        options.mainMaterial.color = colorTbl[colorPaletteNum][randomNum]
    end
    return options
end


--- 立体コメントの生成処理
--- @param pos Vector3 @立体コメントの生成位置
--- @param rot Quaternion @立体コメントの生成回転
--- @param scl number @立体コメントの大きさ
--- @param comment string @立体コメントの文字列
function solidComment.commentDrop(pos, rot, scl, comment)
    local options = {
        position = pos,
        rotation = Quaternion.Euler(0,180,0)*rot,
        scale = scl,
        thickness = scl,
        autoDeleteSeconds = 15,
        fontType = ExportSolidCommentFontType.NotoSans,
        animations = {
            isSpawnAnimationEnabled = false
        },
        mainMaterial = {},
        sideMaterial = {}
    }
    colorPatternOptionSetting(options)
    --options = colorPatternOptionSetting(options)
    vci.vc.room.solidComment.CreateSolidComment(comment, options)
end



--- 色選択コライダーのアクティブ切り替え
--- @param activeFlag boolean @コライダーのアクティブ・非アクティブ
local function colorColliderActiveSwitch(activeFlag)
    for i = 1, 7 do
        colorCollider[i].SetActive(activeFlag)
    end
end

--- パレットオブジェクトの非アクティブ化
function solidComment.inactive()
    vci.assets.GetTransform("ColorPalette").SetActive(false)
    paletteButtonCollider.SetActive(false)
    colorColliderActiveSwitch(false)
end

--- パレットスイッチコライダーのアクティブ・非アクティブ化処理
function solidComment.paletteSwitchActive(activeFlag)
    paletteButtonCollider.SetActive(activeFlag)
end

--- 色選択用コライダーの移動処理
local function colorColliderTransform()
    if not(openFlag) or openAnimationFlag then
        return
    end
    if paletteButtonCollider.IsMine then
        for i = 1, 7 do
            colorCollider[i].SetPosition(colorObj[i].GetPosition())
            colorCollider[i].SetRotation(colorObj[i].GetRotation())
        end
    end
end

--- 選択マークの位置更新処理
local function selectColorUpdate()
    if openFlag then
        selectMark.SetLocalPosition(vci.assets.GetTransform(colorNameTbl[colorPaletteNum]).GetLocalPosition())
        selectMark.SetLocalScale(100*Vector3.one)
    end
end

--- パレットのオープンアニメーション処理
local function openAnimation()
    if openAnimationFlag then
        if os.time() - openAnimationStartTime >= 2 then
            colorColliderActiveSwitch(true)
            openAnimationFlag = false
        end
    end
end

--- パレットのクローズアニメーション処理
local function closeAnimation()
    if closeAnimationFlag then
        if os.time() - closeAnimationStartTime >= 2 then
            vci.assets.GetTransform("ColorPalette").SetActive(false)
            for i = 1, #colorNameTbl do
                colorObj[i].SetLocalScale(100*Vector3.one)
            end
            closeAnimationFlag = false
        end
    end
end

--- 初期化時のState読み込み処理
function solidComment.readState()
    if vci.state.Get("ColorPalette") ~= nil then
        colorPaletteNum = vci.state.Get("ColorPalette")
    end
end

--- main.luaのupdateAllに置く処理
function solidComment.updateAll()
    --オープンアニメーション処理
    openAnimation()
    --クローズアニメーション処理
    closeAnimation()
    --色選択用コライダーの移動処理
    colorColliderTransform()
    --パレットのスイッチコライダーの移動処理
    paletteButtonCollider.SetPosition(paletteButton.GetPosition())
    paletteButtonCollider.SetRotation(paletteButton.GetRotation())
end


--- main.luaのonUseに置く処理
function solidComment.onUse(use)
    --パレットスイッチを押した際の処理
    if use == "PaletteButtonCollider" then
        if openAnimationFlag or closeAnimationFlag then
        else
            openFlag = not(openFlag)
            if openFlag then
                vci.assets.GetTransform("ColorPalette").SetActive(true)
                selectColorUpdate()
                openAnimationFlag = true
                openAnimationStartTime = os.time()
                paletteAnime.PlayFromName("OpenAnimation", false)
            else
                closeAnimationFlag = true
                closeAnimationStartTime = os.time()
                colorColliderActiveSwitch(false)
                paletteAnime.PlayFromName("CloseAnimation", false)
            end
        end
    end

    --色選択用コライダーを押した際の処理
    if use:sub(1, #"ColorCollider") == "ColorCollider" then
        if openFlag and not(openAnimationFlag) then
            colorPaletteNum = tonumber(use:sub(-1))
            vci.state.Set("ColorPalette", colorPaletteNum)
            selectColorUpdate()
        end
    end
end


--- main.luaのonTriggerEnterに置く処理
function solidComment.onTriggerEnter(item, collider)
    --手が、色選択用コライダーに触れた際の処理
    if collider == "HandPointMarker" then
        local vibrationFlag = false
        local colorTouchFlag = false
        if item:sub(1, #"ColorCollider") == "ColorCollider" then
            vibrationFlag = true
            colorTouchFlag = true
        end
        if colorTouchFlag then
            for i = 1, 7 do
                if i == tonumber(item:sub(-1)) then
                    colorObj[i].SetLocalScale(120*Vector3.one)
                else
                    colorObj[i].SetLocalScale(100*Vector3.one)
                end
            end
        end
        if vibrationFlag then
            vci.assets.HapticPulseOnTouchingController(item, 1, 0.1)
        end
    end
end

--- main.luaのonTriggerEnterに置く処理
function solidComment.onTriggerExit(item, collider)
    --手が、色選択用コライダーから離れた際の処理
    if collider == "HandPointMarker" then
        if item:sub(1, #"ColorCollider") == "ColorCollider" then
            colorObj[tonumber(item:sub(-1))].SetLocalScale(100*Vector3.one)
        end
    end
end

return solidComment