
--  s.m_s03_chest_wheel_(1..8)_frame


local wheelPrefix = "seq_chest_anim_wheel_r"
local wheelFrameCount = 32



function Seqence(wheelNumber)
    local s = MystSceneManager:GetInstance():GetCurrentView():GetNode(wheelPrefix..wheelNumber)
    local ss = tolua.cast(s, "MystSprite2DSequence")
    return ss
end

function SetFrame(wheelNumber, frameNumber)
    Seqence(wheelNumber):SetCurrentFrame(frameNumber)
end

function GetFrame(wheelNumber)
    return Seqence(wheelNumber):GetCurrentFrame()
end

-- private:
function RotateOne(wheelNumber, dF)
    
    local newFrame = GetFrame(wheelNumber) + dF
    
    if newFrame < 1 then
        newFrame = newFrame + wheelFrameCount
    end
 
    if newFrame > wheelFrameCount then
        newFrame = newFrame - wheelFrameCount

    end
    PlaySound("snd_s03_0112_rotate")
    SetFrame(wheelNumber, newFrame)
    
    
    if  IsSolved() then
        SolvedEvent()
    end
   
end

function SolvedEvent()
    s.s03_0112_chest_solved = 1
    PlaySound("snd_s03_0112_solved")
    Talk("com_s03_0112_01_hero")  
end

-- private:
function RotateOneSync(wheelNumber, dF)
    OldAction("rot"..wheelNumber..dF, function()
        RotateOne(wheelNumber, dF)
    end)
    Sleep(100)
end

function IsRightSet()
    local right = true
    
    for i = 1, 8 do
        if GetFrame(i) ~= 16 then
            right = false
        end
    end
    
    return right
end


function IsSolved()
    return IsRightSet() 
end
   
-- private:

-- public:

function OnDown(name, n1)

------------------------------------------     
    local number = string.sub(name, string.len("reg_rotate_down_x"), string.len(name))
    number = tonumber(number)
    local dF = 1
    RotateOneSync(number, dF)      
    RotateOneSync(number, dF)
    RotateOneSync(number, dF)   
    RotateOneSync(number, dF) 
end

function OnUp(name)
---- komentarz do obracania
	--[[if s.s03_0112_com02==5 then 
		Talk("com_s03_0112_02_hero")
		s.s03_0112_com02=s.s03_0112_com02+1 
	else
		s.s03_0112_com02=s.s03_0112_com02+1 
	end]]
------------------------------------------
    local number = string.sub(name, string.len("reg_rotate_up_x"), string.len(name))
    number = tonumber(number)
    local dF = -1
    RotateOneSync(number, dF)      
    RotateOneSync(number, dF)
    RotateOneSync(number, dF)   
    RotateOneSync(number, dF)      
end

function OnSceneEnter()
	s.s03_0112_com02=0
    OldInterfaceMap.SetCuttentModule(s03_0112_chest)
    if s.com_s03_0112_03_hero==0 and s.s03_0112_chest_visit==0 then
		Talk("com_s03_0112_03_hero")
	end
	if s.com_s03_0112_04_hero==0 and s.com_s03_0116_01_hero==1 then
		Talk("com_s03_0112_04_hero")
	end
    -- s.s03_0112_chest_visit == 1
    Load()
    
    SetMinigameResolver(function()
        s.s03_0112_chest_solved = 1
        Delete("i_buttons_08")
        Take("i_tilepieces")
        Widgets.Inventory.ShowBigPanel("i_tilepieces")
        s.s03_0112_chest_reg_was_opened = 1
        for i = 1, 8 do
            SetFrame(i, 16)
        end
		s.m_s03_chest_right_solved = 1
		s.m_s03_chest_left_solved = 1
    end)      
end


function Save()
    local sT = OldSavableTable(".m_s03_chest")
    
    for i = 1, 8 do
        sT["wheel"..i] =  GetFrame(i)
    end
end

function Load()
    if SavableTableExists(".m_s03_chest") then
        local sT = OldSavableTable(".m_s03_chest")
        for i = 1, 8 do
            SetFrame(i, sT["wheel"..i])
        end     
    end
end

function OnSave()
    Save()
end

function OnLoad()
    Load()
end

function OnSceneExit()
    Save()
end

