﻿local displayModule = {}

local switchCollider = vci.assets.GetTransform("ButtonRootCollider")
local max = {{5,2,5},{5,7,5}}
local min = {{0,0,0},{-5,0,-5}}

---四捨五入
---@param x number @対象の数字
---@param digit number @有効数字（ex. 10, 1, 0.1...）
---@return number @四捨五入された後の数字
local function shishagonyu(x, digit)
    return (math.floor(x/digit + 0.5))*digit
end


---スイッチのテキストアップデート
---@param switchFase boolean @表示は「出現範囲」か「出現オフセット」か
---@param switchFlag boolean @基準位置がスイッチかどうか。
---@param initialFlag boolean @初期化処理かどうか。オーナー名を入れるかどうかの処理
function displayModule.textUpdate(switchFase, switchFlag, initialFlag)
    local tbl
    if switchFase == 1 then
        tbl = vci.state.Get("Range")
    else
        tbl = vci.state.Get("Offset")
    end
    if switchFase == 1 then
        vci.assets.SetText("PositionNameText", "出現範囲")
    else
        vci.assets.SetText("PositionNameText", "出現オフセット")
    end
    local text = "<line-height=3.3>x : "..tbl[1].."<br>y : "..tbl[2].."<br>z : "..tbl[3]
    vci.assets.SetText("PositionText", text)
    if switchFlag then
        vci.assets.SetText("SwitchText", "スイッチ中心")
        vci.assets.GetTransform("Yajirushi").SetActive(true)
        if not(vci.assets.GetTransform("Yajirushi").GetAnimation().IsPlaying()) then
            vci.assets.GetTransform("Yajirushi").GetAnimation().Play(true)
        end
    else
        vci.assets.SetText("SwitchText", "プレイヤー中心")
        vci.assets.GetTransform("Yajirushi").SetActive(false)
    end
    if initialFlag then
        vci.assets.SetTextAndFitFontSize("OwnerText", vci.vc.room.GetLocalPlayer().GetName(), 0, 1.8)
    end
end


--- Grab中のテキスト数値更新処理
--- @param switchFase boolean @表示は「出現範囲」か「出現オフセット」か
--- @param preScl table @Grabする前のスケール
--- @return table @更新後の数値
function displayModule.textUpdateGrabbing(switchFase, preScl)
    local nowScl = {
        switchCollider.GetLocalScale().x,
        switchCollider.GetLocalScale().y,
        switchCollider.GetLocalScale().z
    }
    local tbl
    if switchFase == 1 then
        tbl = vci.state.Get("Range")
        vci.assets.SetText("PositionNameText", "出現範囲")
    else
        tbl = vci.state.Get("Offset")
        vci.assets.SetText("PositionNameText", "出現オフセット")
    end
    local nxtState = {}
    local text = "<line-height=3.3>"
    for i = 1, 3 do
        --現在値をベースに、スケール変更を加える
        local num = tonumber(tbl[i]) + math.log(nowScl[i]/preScl[i])
        --上限下限チェック
        if num > max[switchFase][i] then
            num = max[switchFase][i]
        elseif num < min[switchFase][i] then
            num = min[switchFase][i]
        end
        --四捨五入処理
        nxtState[i] = tostring(shishagonyu(num, 0.01))
        --0詰め処理
        if #nxtState[i] == 1 then
            nxtState[i] = nxtState[i]..".00"
        elseif #nxtState[i] == 3 then
            nxtState[i] = nxtState[i].."0"
        end
        --テキスト化処理
        if i == 1 then
            text = text.."x : "..nxtState[i].."<br>"
        elseif i == 2 then
            text = text.."y : "..nxtState[i].."<br>"
        elseif i == 3 then
            text = text.."z : "..nxtState[i].."<br>"
        end
    end
    vci.assets.SetText("PositionText", text)
    return nxtState
end


--- 落下基準点を切り替えた際の高さオフセット調整処理
--- @param switchFlag boolean @基準位置がスイッチかどうか。
--- @param switchFase boolean @表示は「出現範囲」か「出現オフセット」か
--- @param adjustHeight number @オフセット量
--- @return string @落下位置のオフセット値（高さ）
function displayModule.baseSwitchHeightAdjust(switchFlag, switchFase, adjustHeight)
    local tbl = vci.state.Get("Offset")
    --補正処理
    if switchFlag then
        tbl[2] = tbl[2] - adjustHeight
    else
        tbl[2] = tbl[2] + adjustHeight
    end
    --四捨五入処理
    tbl[2] = tostring(shishagonyu(tbl[2], 0.01))
    --0詰め処理
    if tbl[2] == 1 then
        tbl[2] = tbl[2]..".00"
    elseif tbl[2] == 3 then
        tbl[2] = tbl[2].."0"
    end
    if switchFase == 2 then
        local text = "<line-height=3.3>"
        --テキスト化処理
        for i = 1, 3 do
            if i == 1 then
                text = text.."x : "..tbl[i].."<br>"
            elseif i == 2 then
                text = text.."y : "..tbl[i].."<br>"
            elseif i == 3 then
                text = text.."z : "..tbl[i].."<br>"
            end
        end
        vci.assets.SetText("PositionText", text)
    end
    return tbl[2]
end

return displayModule