local MAX_VALUE = 350.0

local DV_COMPLETED = 30
    
function GameScreen:RestoreScreen()
    if(GetState(CURRENT_SCREEN, "state") == "") then
        self:add_slider(319, 1299, "slider_01", {{1, 1}, {3, -1}}, nil, 1)
        self:add_slider(319, 1299, "slider_02", {{2, 1}, {1, -1}}, nil, 2)
        self:add_slider(319, 1299, "slider_03", {{3, 1}, {2, -1}}, nil, 3)
         
        self:add_line(1, math.random(180, 210))
        self:add_line(2, math.random(40, 100))
        self:add_line(3, math.random(50, 80))
        SetState(CURRENT_SCREEN, "state", "mg")
    elseif(GetState(CURRENT_SCREEN, "state") == "mg") then
        self:add_slider(319, 1299, "slider_01", {{1, 1}, {3, -1}}, tonumber(GetState(CURRENT_SCREEN, "slider_1")), 1)
        self:add_slider(319, 1299, "slider_02", {{2, 1}, {1, -1}}, tonumber(GetState(CURRENT_SCREEN, "slider_2")), 2)
        self:add_slider(319, 1299, "slider_03", {{3, 1}, {2, -1}}, tonumber(GetState(CURRENT_SCREEN, "slider_3")), 3)
         
        self:add_line(1, tonumber(GetState(CURRENT_SCREEN, "line_1")))
        self:add_line(2, tonumber(GetState(CURRENT_SCREEN, "line_2")))
        self:add_line(3, tonumber(GetState(CURRENT_SCREEN, "line_3")))
    elseif(GetState(CURRENT_SCREEN, "state") == "finish") then
        clApplication:LoadLocation("005_security")
    end
end

function GameScreen:InitScreen()
    self:LoadScreen()       
     
     self.sliders = {}
     
     self:RestoreScreen()
     
     HUDScreen:SetMiniGameMode()
      
     HUDScreen:ShowHintText({path = "graphics/minigames/05_voltage/hint_05a", x = 428, y = 1128})
        
     MusicPlay({"sound/ambient/05_ambient.ogg", "sound/music/hospital.ogg"}, 5) 
     
     HUDScreen:CreateBackButton("005_security")
end

function GameScreen:UpdateLine(line, dv)
    dv = dv or 0;
    --count = count or 0
    --SetState("DEBUG", "line_updatr"..count, ""..dv)
    --count = count + 1
    
    line.value = line.value + dv;
    if(line.value > MAX_VALUE) then line.value = MAX_VALUE end
    if(line.value < 0) then line.value = 0 end
   
   
    ItemSetScaleY(line.name, line.value)
    if(line.value / MAX_VALUE < 0.3) then
        ItemSetColor(line.name, 1.0, 0.0, 0.0)
    elseif(line.value / MAX_VALUE > 0.7) then
        ItemSetColor(line.name, 0.0, 1.0, 0.0)
    else
        ItemSetColor(line.name, 1.0 - (line.value - MAX_VALUE * 0.3) / (MAX_VALUE * 0.4), (line.value - MAX_VALUE * 0.3) / (MAX_VALUE * 0.4), 0.0)
    end
    SetState(CURRENT_SCREEN, "line_"..line.index, ""..line.value)
end

function GameScreen:add_line(line_num, start_value)
    self.lines = self.lines or {}
    
    local line = {}
    line.name = "display_0"..line_num;
    line.value = start_value
    line.index = line_num;
    ItemSetHotSpot("display_0"..line_num, 71, 1)
    ItemSetOpacity("display_0"..line_num, 0.6)
    self:UpdateLine(line)
    table.insert(self.lines, line)
end

function GameScreen:add_slider(min, max, slider_name, lines, start_value, index)
    local slider = {}
    slider.min_pos = min;
    slider.max_pos = max;
    slider.name = slider_name;
    slider.lines = lines;
    slider.index = index;
    if(start_value) then
        ItemSetPosition(slider.name, start_value, ItemGetY(slider.name))
    end
    
    table.insert(self.sliders, slider)
end

function GameScreen:get_line_by_name(item_name)
    for key, line in pairs(self.lines) do
        if(line.name == item_name) then
            return line;
        end
    end
    
    return nil;
end


function GameScreen:get_slider_by_name(item_name)
    for key, slider in pairs(self.sliders) do
        if(slider.name == item_name) then
            return slider;
        end
    end
    
    return nil;
end

function GameScreen:onObjectClick(item_name)
    if(self.active == false) then return end
    
    if(self.drag_slider ) then
        self.drag_slider = nil;
    else
        local slider = self:get_slider_by_name(item_name);
        
        if(slider) then
            self.drag_slider = slider;
            self.drag_slider.dx = ItemGetX(self.drag_slider.name) - messenger_table.mouse_X;
        end
    end
end

function GameScreen:OnMouseUp(item_name)
    if(self.drag_slider) then
        --SetState(CURRENT_SCREEN, "slider_"..self.drag_slider.index, ""..ItemGetX(self.drag_slider.name))
        self.drag_slider = nil;
        self.old_pos = nil
    end
    
    if(self:IsMinigameCompleted())then
        self:OnFinish();
    end
end

function GameScreen:_skip_update()
    local fl = false
    for key, line in pairs(self.lines) do
        if(line.value < MAX_VALUE / 2 - DV_COMPLETED) then
            self:UpdateLine(line, 2)
            fl = true
        elseif(line.value > MAX_VALUE / 2 + DV_COMPLETED)then
            self:UpdateLine(line, -2)
            fl = true
        end
    end
    
    if(fl) then
        DelayedExec(0.005, function()
            self:_skip_update()
        end)
    end
end

function GameScreen:OnSkip()
    --self:_skip_update()
    
    DelayedExec(1.0, function()
        self:OnFinish();
    end, 113)
end

function GameScreen:IsMinigameCompleted()   
    for key, line in pairs(self.lines) do
        if(line.value < MAX_VALUE / 2 - DV_COMPLETED or line.value > MAX_VALUE / 2 + DV_COMPLETED) then
            return false;
        end
    end
    
    return true
end

function GameScreen:OnFinish()
    if(self.active == false) then return end
    HUDScreen:SetInvntoryOnlyMode()
    ItemChangeOpacity("lamp_on", 1.0, 0.3)
    SoundPlay("sound/lamp_on.ogg");
    SetState("screen_005_security", "state", "finish")
    SetState("screen_002_hall", "state", "elevator")
    self.active = false;
    DelayedExec(2.0, function()
        clApplication:LoadLocation("005_security")
    end, 113)
end

function GameScreen:Update()
    if(clApplication.game_pause) then return end
    
    if(self.drag_slider) then

        ItemSetPosition(self.drag_slider.name, messenger_table.mouse_X + self.drag_slider.dx, ItemGetY(self.drag_slider.name));
        if(ItemGetX(self.drag_slider.name) < self.drag_slider.min_pos) then
            ItemSetPosition(self.drag_slider.name, self.drag_slider.min_pos, ItemGetY(self.drag_slider.name))
        end
        if(ItemGetX(self.drag_slider.name) + ItemGetWidth(self.drag_slider.name) > self.drag_slider.max_pos) then
            ItemSetPosition(self.drag_slider.name, self.drag_slider.max_pos - ItemGetWidth(self.drag_slider.name), ItemGetY(self.drag_slider.name))
        end
        self.old_pos = self.old_pos or ItemGetX(self.drag_slider.name);
        
        if(math.abs(ItemGetX(self.drag_slider.name) + ItemGetWidth(self.drag_slider.name) / 2 - messenger_table.mouse_X) > 70
            or math.abs(ItemGetY(self.drag_slider.name) + ItemGetHeight(self.drag_slider.name) / 2 - messenger_table.mouse_Y) > 40) then
            SetState(CURRENT_SCREEN, "slider_"..self.drag_slider.index, ""..ItemGetX(self.drag_slider.name))
            self.drag_slider = nil
            self.old_pos = nil
            return
        end
        
        if(self.old_pos ~= ItemGetX(self.drag_slider.name)) then
            for key, value in pairs(self.drag_slider.lines) do
                self:UpdateLine(self:get_line_by_name("display_0"..value[1]), value[2] * (self.old_pos - ItemGetX(self.drag_slider.name)) * 0.5)
            end
            self.old_pos = ItemGetX(self.drag_slider.name);
        end        
    end
end
------------------------------------------------------------------------------------------------------
-------------------------------------------Mouse functions--------------------------------------------
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
function GameScreen:LoadScreen()
	--[[---Background---]]--
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "01_patch", "graphics/minigames/05_voltage/05_voltage", 0, 0);
	
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slider_zone_01", "graphics/minigames/05_voltage/slider_zone_01", 319, 1010);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slider_zone_02", "graphics/minigames/05_voltage/slider_zone_02", 319, 935);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slider_zone_03", "graphics/minigames/05_voltage/slider_zone_03", 319, 875);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "display_03", "graphics/minigames/05_voltage/display_01", 1189, 761);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "display_02", "graphics/minigames/05_voltage/display_01", 759, 761);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "display_01", "graphics/minigames/05_voltage/display_01", 319, 761);
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slider_01", "graphics/minigames/05_voltage/slider_01", 932, 986);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slider_02", "graphics/minigames/05_voltage/slider_02", 764, 913);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slider_03", "graphics/minigames/05_voltage/slider_03", 1052, 835);
     
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "lamp_on", "graphics/minigames/05_voltage/lamp_on", 1377, 537);
    ItemSetOpacity("lamp_on", 0.0)
 
    clScreenManager:add_screen("hud") 
end