--require("scripts/GameObjects")
require("scripts/gui/Tips")
require("scripts/gui/HintText")

local hint_frames_amount = 78
local skip_frames_amount = 78

function GameScreen:RestoreScene()
    --clInventory:RestoreScene()
end

function GameScreen:InitScreen()
    if(self.state ~= "already_load") then --запрещаем перезагрузку от уровня к уровню
        self.state = "already_load"
        
        self.hud_mode = nil
        self.skip_active = false;
        self.hint_active = true;
        self.hint_frame = nil
        self.skip_frame = nil
        self.back_screen = nil;
        
        self:LoadScreen()
        self:RestoreScene()
        HINT_TIME_ACTIVATE = 0.01
        self:SetDefaultMode(true);
        clScreenManager:add_screen("inventory") 
        clScreenManager:add_screen("drag_object") 
    else
        ItemSetDepth(self.name, LAYERS.FRONT_MOST)
        ItemSetDepth("menu_button_screen", LAYERS.FRONT_MOST)
        ItemSetDepth("drag_object", LAYERS.FRONT_MOST)
        ItemSetDepth("inventory", LAYERS.FRONT_MOST)
        
        self:SetDefaultMode();
    end
    
   
end
--[[
function GameScreen:HideSkip()
    if(self.hud_mode ~= "minigame") then return end
    
    self.hud_mode = "none"
    ItemRemove(self.name, "skip", 0.3)
    ItemRemove(self.name, "skip_empty", 0.3)
    ItemRemove(self.name, "skip_anim", 0.3)
end
--]]
function GameScreen:OnClick(item_name)
    if(clApplication.game_pause) then return end
    
    Tip:Hide();
    HintTest:Hide();
    
    if(item_name == "skip" and self.skip_active) then
        if(CurrentScreen.OnSkip) then
            SoundPlay("sound/interface/button.ogg")
            CurrentScreen:OnSkip();
        end
    end
    
    if(item_name == "close_minigame" and self.back_screen) then
        SoundPlay("sound/interface/button.ogg")
        Inventory:ActiveToolDrop()
        clApplication:LoadLocation(self.back_screen)
    end
    
    if(item_name == "hint" and self.hint_active) then
        if(CurrentScreen:OnHint()) then
            SoundPlay("sound/interface/hint_skip.ogg")
            self.hint_active = false;
            self:HintReInit();
        end
    end
    
    if(item_name == "menu_button") then
        SoundPlay("sound/interface/button.ogg")
        SetVisibleCursor(true)
        ItemSetOpacity("drag_object", 0.0)
        ItemSetOpacity("hud", 0.0)
        ItemSetOpacity("menu_button", 0.0)
        ItemSetOpacity("inventory", 0.0)
        local ingame_menu = clScreenManager:add_screen("ingame_menu") 
        self.ingame_menu = true
    end
    
end

function GameScreen:OnKeyDown(key)
    if(clApplication.game_pause) then return end
    
	if(key == VK_ESCAPE) then
	    SetVisibleCursor(true)
        ItemSetOpacity("drag_object", 0.0)
        
	    messenger_table.on_key_down = "";
		ItemSetOpacity("hud", 0.0)
        ItemSetOpacity("menu_button", 0.0)
        ItemSetOpacity("inventory", 0.0)
        local ingame_menu = clScreenManager:add_screen("ingame_menu") 
	end
end

function GameScreen:SetMiniGameMode()
    if(self.hud_mode == "minigame") then return end
    
    self.skip_active = false; 
    self.hud_mode = "minigame";
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "skip", "graphics/interface/hud/skip", 1392, 1104);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "skip_empty", "graphics/interface/hud/hint_skip_empty", 1392, 1104);
    self:CreateAnim(OBJ_TYPE.NO_ACTIVE, "skip_anim", "graphics/interface/hud/skip_anim", 1392, 1140);
    self:RemoveHint();
    self:RemoveComicsSkip();
    ItemSetOpacity("skip", 0.0)
    self:SkipInit();
    ItemSetOpacity("hud", 1.0)
end

function GameScreen:SetDefaultMode(hint_active)
    if(self.hud_mode == "default") then return end
    hint_active = self.hint_active or hint_active;
    
    self.hud_mode = "default";
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "hint", "graphics/interface/hud/hint", 1392, 1104);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "hint_empty", "graphics/interface/hud/hint_skip_empty", 1392, 1104);
    self:CreateAnim(OBJ_TYPE.NO_ACTIVE, "hint_anim", "graphics/interface/hud/hint_anim", 1392, 1140);
    self:RemoveComicsSkip()
    self:RemoveSkip();
    
    if(hint_active) then
        self.hint_active = true; 
        ItemSetOpacity("hint_empty", 0.0)
        ItemSetOpacity("hint_anim", 0.0)
    else
        self.hint_active = false; 
        
        ItemSetOpacity("hint", 0.0)
        self:HintInit();
    end
    
    ItemSetOpacity("hud", 1.0)
end

function GameScreen:SetComicsMode()
    if(self.hud_mode == "comics") then return end
    
    self.hud_mode = "comics";
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "comics_skip", "graphics/interface/hud/skip", 1392, 1104);
    
    self:RemoveSkip();
    self:RemoveHint();
    ItemSetOpacity("hud", 1.0)
end

function GameScreen:SetInvntoryOnlyMode()
    self.hud_mode = "inv";
    
    ItemSetOpacity("hud", 1.0)
    self:RemoveSkip();
    self:RemoveHint();
    self:RemoveComicsSkip();
end

function GameScreen:SetMenuOnlyMode()
    self.hud_mode = "menu";
    self:RemoveSkip();
    self:RemoveHint();
    self:RemoveComicsSkip();
    ItemSetOpacity("hud", 0.0)
end

function GameScreen:SetHUDMode(mode)
    if(mode == "comics") then
        self:SetComicsMode();
    elseif(mode == "default") then
        self:SetDefaultMode();
    elseif(mode == "minigame") then
        self:SetMiniGameMode();   
    end
end

function GameScreen:GetHUDMode()
    return self.hud_mode;
end

function GameScreen:OnMouseUp(item_name)
end

function GameScreen:OnMouseEnter(item_name)
end

function GameScreen:OnMouseLeave(item_name)
end

function GameScreen:OnMouseMove(item_name)
end

function GameScreen:OnButtonDown(button_name)
end

function GameScreen:Update()
end

function GameScreen:CreateBackButton(back_screen)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "close_minigame", "graphics/interface/hud/close_minigame", 1504, 1007);
    self.back_screen = back_screen;
end

function GameScreen:RemoveBackButton()
    ItemRemove(self.name, "close_minigame")
    self.back_screen = nil;
end

function GameScreen:LoadHO_HUD(counter_type)
    if(counter_type) then
        self:CreateObj(OBJ_TYPE.NO_ACTIVE, "ho_panel", "graphics/interface/hud/counter", 19, 1104);
    else
        self:CreateObj(OBJ_TYPE.NO_ACTIVE, "ho_panel", "graphics/interface/hud/list_bar", 8, 758);
    end
end

function GameScreen:UnLoadHO_HUD(time)
    ItemRemove(self.name, "ho_panel", time)
end

function GameScreen:LoadScreen()
    cpp_application_add_window("menu_button_screen");
    cpp_create_sprite("menu_button_screen", OBJ_TYPE.ACTIVE_ALL, "menu_button", "graphics/interface/hud/menu", 1390, 18);
end

------------------------------------------------------------------
function GameScreen:HintInit()
    self.hint_frame = self.hint_frame or 1;
    AnimationStop(self.name, "hint_anim")
    AnimationSetFrame(self.name, "hint_anim", self.hint_frame)
    local hint_step_time = HINT_TIME_ACTIVATE / hint_frames_amount;
    DelayedExec(hint_step_time, function()
        self:HintUpdate(hint_step_time)
    end, 113)
end

------------------------------------------------------------------
function GameScreen:HintReInit()
    ItemChangeOpacity("hint", 0.0, 0.2)
    ItemChangeOpacity("hint_empty", 1.0, 0.2)
    ItemChangeOpacity("hint_anim", 1.0, 0.2)
        
    self.hint_frame = 1;
    AnimationStop(self.name, "hint_anim")
    AnimationSetFrame(self.name, "hint_anim", self.hint_frame)
    local hint_step_time = HINT_TIME_ACTIVATE / hint_frames_amount;
    if(self.hint_frame >= hint_frames_amount) then
        ItemChangeOpacity("hint", 1.0, 0.2)
        ItemChangeOpacity("hint_empty", 0.0, 0.2)
        ItemChangeOpacity("hint_anim", 0.0, 0.2)
    
        self.hint_active = true;
    else
        DelayedExec(hint_step_time, function()
            self:HintUpdate(hint_step_time)
        end, 113)
    end
end
-----------
function GameScreen:HintUpdate(_time)
    if(not self.hint_frame) then return end
    if(self.hud_mode ~= "default") then return end
    
    self.hint_frame = self.hint_frame + 1;
    AnimationSetFrame(self.name, "hint_anim", self.hint_frame)
    
    if(self.hint_frame >= hint_frames_amount) then
        SoundPlay("sound/interface/hint_skip_active.ogg")
        ItemChangeOpacity("hint", 1.0, 0.2)
        ItemChangeOpacity("hint_empty", 0.0, 0.2)
        ItemChangeOpacity("hint_anim", 0.0, 0.2)
    
        self.hint_active = true;
        return;
    end
    local hint_step_time = HINT_TIME_ACTIVATE / hint_frames_amount;
    DelayedExec(hint_step_time, function()
        self:HintUpdate(hint_step_time)
    end, 113)
end


------------------------------------------------------------------
function GameScreen:SkipInit()
    ItemChangeOpacity("skip", 0.0, 0.2)
    ItemChangeOpacity("skip_empty", 1.0, 0.2)
    ItemChangeOpacity("skip_anim", 1.0, 0.2)
    
    self.skip_frame = self.skip_frame or 1;
    AnimationStop(self.name, "skip_anim")
    AnimationSetFrame(self.name, "skip_anim", self.skip_frame)
    local skip_step_time = SKIP_TIME_ACTIVATE / skip_frames_amount;
    if(self.skip_frame >= skip_frames_amount) then
        ItemChangeOpacity("skip", 1.0, 0.2)
        ItemChangeOpacity("skip_empty", 0.0, 0.2)
        ItemChangeOpacity("skip_anim", 0.0, 0.2)
        self.skip_active = true;
    else
        DelayedExec(skip_step_time, function()
            self:SkipUpdate(skip_step_time)
        end, 113)
    end
end

------------------------------------------------------------------
function GameScreen:SkipReInit()
    self.skip_frame = self.skip_frame or 1;
    AnimationStop(self.name, "skip_anim")
    AnimationSetFrame(self.name, "skip_anim", self.skip_frame)
    local skip_step_time = SKIP_TIME_ACTIVATE / skip_frames_amount;
    DelayedExec(skip_step_time, function()
        self:SkipUpdate(skip_step_time)
    end, 113)
end
-----------
function GameScreen:SkipUpdate(_time)
    if(self.hud_mode ~= "minigame") then return end
    
    self.skip_frame = self.skip_frame + 1;
    AnimationSetFrame(self.name, "skip_anim", self.skip_frame)
    
    if(self.skip_frame >= skip_frames_amount) then
        SoundPlay("sound/interface/hint_skip_active.ogg")
        ItemChangeOpacity("skip", 1.0, 0.2)
        ItemChangeOpacity("skip_empty", 0.0, 0.2)
        ItemChangeOpacity("skip_anim", 0.0, 0.2)
        self.skip_active = true;
        return;
    end
    
    DelayedExec(_time, function()
        self:SkipUpdate(_time)
    end, 113)
end
 
function GameScreen:ShowTips(tip)
    Tip:Show(tip.path, tip.x, tip.y, tip.end_fun)
end

function GameScreen:ShowHintText(hint_text)
    HintTest:Show(hint_text.path, hint_text.x, hint_text.y, hint_text.end_fun, hint_text.bar_pos, hint_text.loop)
end

function GameScreen:ShowCrystalCounter()
    Tip:Remove()
    HintTest:Remove()
    
    local count = GetState("GLOBAL", "crystal")
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "crystal_panel", "graphics/interface/hud/hint_bar", 296, 1096);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "crystal_text", "graphics/interface/crystals/crystal_"..count, 100, 100)
    ItemSetPosition("crystal_text", 426, 1127)
    DelayedExec(2.0, function()
        ItemRemove(self.name, "crystal_panel", 0.5)
        ItemRemove(self.name, "crystal_text", 0.5)
    end, 123)
end

function GameScreen:RemoveCrystalCounter()
    ItemRemove(self.name, "crystal_panel")
    ItemRemove(self.name, "crystal_text")
    StopRequests(123)    
end

function GameScreen:RemoveScreen()
    self.hud_mode = nil
    cpp_application_remove_window("menu_button_screen");
end

------------------------------------------------------------------------------
function GameScreen:Hide(_time)
    if(_time) then
        ItemChangeOpacity(self.name, 0.0, _time)
    else
        ItemSetOpacity(self.name, 0.0)
    end
end

function GameScreen:Show()
    if(_time) then
        ItemChangeOpacity(self.name, 1.0, _time)
    else
        ItemSetOpacity(self.name, 1.0)
    end
end

function GameScreen:RemoveHint()
    ItemRemove(self.name, "hint");
    ItemRemove(self.name, "hint_empty");
    ItemRemove(self.name, "hint_anim");
end

function GameScreen:RemoveSkip()
    ItemRemove(self.name, "skip");
    ItemRemove(self.name, "skip_anim")
    ItemRemove(self.name, "skip_empty")
end

function GameScreen:RemoveComicsSkip()
    ItemRemove(self.name, "comics_skip")
end
------------------------------------------------------------------------------