min = 0.35
max = 0.6

function SetSliderValue(normValue)
    if normValue>=1 then
        s01_0304_burner.spr_s01_0304_slider.self:SetScale(Ogre.Vector2(1,1))
        s01_0304_burner.spr_s01_0304_slider_top.self:SetScale(Ogre.Vector2(1,1))              
        return
    end
    if normValue<=0 then
        s01_0304_burner.spr_s01_0304_slider.self:SetScale(Ogre.Vector2(0,1))
        s01_0304_burner.spr_s01_0304_slider_top.self:SetScale(Ogre.Vector2(0,1))
        return
    end
    s01_0304_burner.spr_s01_0304_slider.self:SetScale(Ogre.Vector2(normValue,1))
    s01_0304_burner.spr_s01_0304_slider_top.self:SetScale(Ogre.Vector2(normValue,1))
end

function SetProgress(maxProgresses)
    s01_0304_burner.spr_s01_0304_progress.self:SetScale(Ogre.Vector2(s.m_s01_burner_counter / maxProgresses,1))
    
    if s.m_s01_burner_counter==maxProgresses then
         s.m_s01_burner_state = 5
         s.s01_0304_burner_solved = 1
         print("--")
         Audio:Stop("snd_s01_0304_burner")
         Talk("com_s01_0304_04_hero")
    end
end

function GetSliderValue()
    return s01_0304_burner.spr_s01_0304_slider.self:GetScale().x
end


function GetTime()
   return MystTimeManager:GetInstance():GetTime()
end

function SetPosition(value)
    local startAngle = -11
    local stopAngle = 0
    
    local startPos = Ogre.Vector2(0.2765624, 0.07638891)
    local stopPos = Ogre.Vector2(0.615624964, 0)    
    
    local deg = startAngle + (stopAngle - startAngle)*value
    s01_0304_burner["spr_s01_0304_sprite"].self:SetAngle(Ogre.Degree(deg))
    s01_0304_burner["spr_s01_0304_sprite"].self:SetPosition(startPos + ((stopPos - startPos) * value))
    
    s01_0304_burner["spr_s01_0304_sprite_empty"].self:SetAngle(Ogre.Degree(deg))
    s01_0304_burner["spr_s01_0304_sprite_empty"].self:SetPosition(startPos + ((stopPos - startPos) * value))    
end

function VPlay(movieName, afterFunction)
    local direction = -1       
    if movieName == "s01_0304_burner_animation_left_full_0" then
        direction = -1
        s.m_s01_burner_state_anim_full = 1
    end  
    
     if movieName == "s01_0304_burner_animation_left_0" then
        direction = -1
        s.m_s01_burner_state_anim_empty = 1
    end 
    
       if movieName == "s01_0304_burner_animation_right_0" then
        direction = 1
         s.m_s01_burner_state_anim_full = 1
    end 
     
    StartNormalTask(800, function (value)
        if direction == -1 then
            value = 1 - value
        end
      
        SetPosition(value)
    end, nil, function ()
        if afterFunction then
            afterFunction()
        end
        s.m_s01_burner_state_anim_full = 0
        s.m_s01_burner_state_anim_empty = 0
    end)
   
end

function LeftAnim(full, afterFunction)
        movieName = "s01_0304_burner_animation_left_0"
        if full then
            movieName = "s01_0304_burner_animation_left_full_0"
        end
        --Video:Seek(movieName, 0)
        --Video:Pause(movieName)

        VPlay(movieName, afterFunction)
       
end


function  OnUpdateDt(dt)
    
    if s.m_s01_burner_state == 2 or s.m_s01_burner_state == 100  then
        if s.m_s01_burner_fire_turned_on == 1 then
          --  s.m_s01_burner_timer1 = s.m_s01_burner_timer1 + dt
            SetSliderValue(GetSliderValue()+(dt*0.0000004))
        else
           -- s.m_s01_burner_timer1 = s.m_s01_burner_timer1 - (dt * 2)
            SetSliderValue(GetSliderValue()-(dt*0.00000016))
        end
    end
    
    s.m_s01_burner_timer2 = s.m_s01_burner_timer2 + dt
    
    if  s.m_s01_burner_state == 2 or s.m_s01_burner_state == 100 then
        value = GetSliderValue()
        if value>max or value<min then
            s.m_s01_burner_middle = 0
        else
            s.m_s01_burner_middle = 1
        end
            
        if  s.m_s01_burner_state == 2 then
            if value > max then
                if s.m_s01_burner_state ~= 1 and  s.m_s01_burner_state ~= 0  then
                    s.m_s01_burner_state = 100
                    s.m_s01_burner_counter = 0
                    PlaySound("snd_s01_0304_i_testtube_break")
                    s01_0304_burner.s01_0304_burner.SetProgress(6)
                    Sleep(1500)
                    
                    PlaySound("snd_s01_0304_move_left")
                    Talk("com_s01_0304_01_hero")
                    OldAction("dfdf",function ()
                       s.m_s01_burner_state = 0
                    end)
                   
                    OldAction("sdf", function ()
                        s01_0304_burner.s01_0304_burner.SetSliderValue(0)
                        LeftAnim(false, function()
                            s.m_s01_burner_counter = 0
                        end )
                    end)
                end
           end
        end
        
        if  s.m_s01_burner_state == 100 then
            if value > max + 0.2 then
                s.m_s01_burner_state = 3
            end 
        end
        
        
        
    end
end

local lastTime = nil
function  OnUpdate()
    if lastTime == nil then
        lastTime = GetTime()
        return
    end
 --  if s.spr_s01_0304_burner_near_probe == 1 then
    newTime = GetTime()
    OnUpdateDt( newTime  - lastTime)
    lastTime = newTime
 --  end
end -- 0,00001

function OnSceneExit()
    Audio:Stop("snd_s01_0304_burner")
end

function OnShow()
    if s.m_s01_burner_fire_turned_on == 1 and s.m_s01_burner_state ~= 5 then
        Audio:Play("snd_s01_0304_burner", true)
    end
end

function  OnSceneEnter()
   SetSliderValue(0)
   SetProgress(6)
   SetMinigameResolver(function()
        s.m_s01_burner_state = 5
        s.s01_0304_burner_solved = 1
        Audio:Stop("snd_s01_0304_burner")
   end)
end