local min_slider = 710
local max_slider = 1220
local slider_size = max_slider - min_slider;

local max_volume = 1000; 

function GameScreen:InitScreen()
    self:LoadScreen()
    
    self.disable = true;
    DelayedExec(0.5, function()
        self.disable = false;
    end)
    self.custom_cursor = IsCustomCursor();
    
    self:FadeOut();
    
    if(self.custom_cursor) then
        ItemSetOpacity("system_cursor_check", 0.0);
    end
    
    self.start_fullscreen = IsFullScreen()
    self.start_custom_cursor = IsCustomCursor();
    self.start_sound_volume = GetState("OPTIONS", "sound_volume")
    self.start_music_volume = GetState("OPTIONS", "music_volume")
    
    self.fullscreen = IsFullScreen()
    if(self.fullscreen) then
        ItemSetOpacity("fullscreen_check", 1.0);
    else
        ItemSetOpacity("fullscreen_check", 0.0);
    end
    self:update_sliders();
end

function GameScreen:update_sliders()
    local sound_volume = tonumber(GetState("OPTIONS", "sound_volume"))
    local music_volume = tonumber(GetState("OPTIONS", "music_volume"))
    
    ItemSetPosition("sound_volume_slider", min_slider + (slider_size / max_volume) * sound_volume, ItemGetY("sound_volume_slider"))
    ItemSetPosition("music_volume_slider", min_slider + (slider_size / max_volume) * music_volume, ItemGetY("music_volume_slider"))
end

function GameScreen:OnKeyDown(key)
    if(self.disable) then return end
    
    if(key == VK_ESCAPE and clApplication.curret_win.name == self.name) then
        self:OnClick("cancel")
    end
    if(key == VK_ENTER and clApplication.curret_win.name == self.name) then
        self:OnClick("ok")
    end
end

function GameScreen:OnClick(item_name)
    if(self.disable) then return end

    if("ok" == item_name) then
        SoundPlay("sound/interface/button.ogg")
        self.disable = true;
        self:FadeIn(0.5);
        if(self.end_fun) then self:end_fun() end
        
        DelayedExec(0.7, function() 
            clScreenManager:remove_screen(self); 
        end)
        
        return;
    end
    if("cancel" == item_name and not self.full_buf) then
        SetState("OPTIONS", "sound_volume", ""..self.start_sound_volume)
        SetState("OPTIONS", "music_volume", ""..self.start_music_volume)
            
        if(self.start_fullscreen ~= IsFullScreen()) then
            ToggleFullScreen();
        end
        if(self.start_custom_cursor ~= IsCustomCursor()) then
            ToggleCustomCursor();
        end
        SetSoundVolume(tonumber(self.start_sound_volume))
        SetMusicVolume(tonumber(self.start_music_volume))
        
        SoundPlay("sound/interface/button.ogg")
        self.disable = true;
        self:FadeIn(0.5);
        if(self.end_fun) then self:end_fun() end
        DelayedExec(0.7, function() 
            clScreenManager:remove_screen(self); 
        end)
        
        return;
    end
    
    if("system_cursor_checkbox" == item_name) then
        SoundPlay("sound/interface/button.ogg")
        ToggleCustomCursor();
        if(self.custom_cursor) then
            self.custom_cursor = false
            ItemSetOpacity("system_cursor_check", 1.0);
        else
            self.custom_cursor = true
            ItemSetOpacity("system_cursor_check", 0.0);
        end
        
        return;
    end
    
    if("fullscreen_checkbox" == item_name) then
        SoundPlay("sound/interface/button.ogg")
        local old_fullscreen = IsFullScreen();
        ToggleFullScreen();
        self.fullscreen = IsFullScreen()
        if(self.fullscreen) then
            ItemSetOpacity("fullscreen_check", 1.0);
        else
            ItemSetOpacity("fullscreen_check", 0.0);
        end
        if(self.fullscreen == old_fullscreen and self.fullscreen == true) then
            self:FadeIn(0.5);
            clApplication.curret_win = clScreenManager:add_screen("wrong_windowed_screen") 
            clApplication.curret_win.end_fun = function()
                clApplication.curret_win = self;
                self:FadeOut(0.5);
            end
        end
        self.full_buf = true;
        DelayedExec(0.2, function()
            self.full_buf = nil
        end)
        return;
    end
    
    if("sound_volume_slider" == item_name) then
        self.drag_slider = {}
        self.drag_slider.name = "sound_volume_slider";
        self.drag_slider.dx = messenger_table.mouse_X - ItemGetX(self.drag_slider.name);  
    end
    if("music_volume_slider" == item_name) then
        self.drag_slider = {}
        self.drag_slider.name = "music_volume_slider";
        self.drag_slider.dx = messenger_table.mouse_X - ItemGetX(self.drag_slider.name);  
    end
    
    if(self.drag_slider == nil) then
        if(messenger_table.mouse_X > min_slider and messenger_table.mouse_X < max_slider + ItemGetWidth("music_volume_slider")) then
            local x_pos = messenger_table.mouse_X - ItemGetWidth("music_volume_slider") / 2;
            if(x_pos < min_slider) then x_pos = min_slider end
            if(x_pos > max_slider) then x_pos = max_slider end
            if(messenger_table.mouse_Y > 590 and messenger_table.mouse_Y < 630) then
                ItemSetPosition("music_volume_slider", x_pos, ItemGetY("music_volume_slider"))
                SetMusicVolume((ItemGetX("music_volume_slider") - min_slider) / (slider_size / max_volume))
            end
            if(messenger_table.mouse_Y > 490 and messenger_table.mouse_Y < 530) then
                ItemSetPosition("sound_volume_slider", x_pos, ItemGetY("sound_volume_slider"))
                SetSoundVolume((ItemGetX("sound_volume_slider") - min_slider) / (slider_size / max_volume))
            end
        end
    end  
end

function GameScreen:OnMouseUp(item_name)
    if(self.drag_slider) then
        self.drag_slider = nil;
        return;
    end
end

function GameScreen:Update()
    if(self.drag_slider) then
        local x_pos = messenger_table.mouse_X  - self.drag_slider.dx;
        if(x_pos < min_slider) then x_pos = min_slider end
        if(x_pos > max_slider) then x_pos = max_slider end
                
        ItemSetPosition(self.drag_slider.name, x_pos, ItemGetY(self.drag_slider.name));
        
        if(self.drag_slider.name == "music_volume_slider")then
            SetMusicVolume((x_pos - min_slider) / (slider_size / max_volume))
            if(messenger_table.mouse_Y < 572 or messenger_table.mouse_Y > 572 + ItemGetHeight("music_volume_slider")) then
                self.drag_slider = nil; 
            end
        end
        if(self.drag_slider.name == "sound_volume_slider")then
            SetSoundVolume((x_pos - min_slider) / (slider_size / max_volume))
            if(messenger_table.mouse_Y < 476 or messenger_table.mouse_Y > 476 + ItemGetHeight("sound_volume_slider")) then
                self.drag_slider = nil; 
            end
        end
        
        if(messenger_table.mouse_X < min_slider or messenger_table.mouse_X > max_slider + ItemGetWidth("music_volume_slider")) then
            self.drag_slider = nil; 
        end
       
    end
end

function GameScreen:LoadScreen()
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "options_back_trigger", "graphics/trigger", 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
    ItemSetOpacity("options_back_trigger", 0.85)
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "options_window", "graphics/interface/menu/options_window", 267, 387);
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "sound_volume_slider", "graphics/interface/menu/sound_volume_slider", 100, 475);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "music_volume_slider", "graphics/interface/menu/sound_volume_slider", 1220, 572);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fullscreen_checkbox", "graphics/interface/menu/fullscreen_checkbox", 712, 653);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "system_cursor_checkbox", "graphics/interface/menu/system_cursor_checkbox", 713, 749);
    
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "system_cursor_check", "graphics/interface/menu/system_cursor_check", 721, 773);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "fullscreen_check", "graphics/interface/menu/fullscreen_check", 721, 674);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "cancel", "graphics/interface/menu/cancel", 286, 861);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "ok", "graphics/interface/menu/ok", 801, 856);
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "options_header", "graphics/interface/menu/options_header", 432, 111);
end
