require("scripts/framework/mgt_hidden_object")
require("scripts/framework/mgt_find_and_apply")
require("scripts/framework/mgt_find_and_apply2")


crystall_found_eff = {
	    {key=EFFECTOR_MODIFIER.KEY_SCALE_XY, start_time = 0.0, time = 0.3, end_value = 1.1},
	    {key=EFFECTOR_MODIFIER.KEY_SCALE_XY, start_time = 0.3, time = 0.3, end_value = 1.0},
	    {key=EFFECTOR_MODIFIER.KEY_SCALE_XY, start_time = 0.6, time = 0.3, end_value = 1.1},
	    {key=EFFECTOR_MODIFIER.KEY_SCALE_XY, start_time = 0.9, time = 0.3, end_value = 1.0},
	    {key=EFFECTOR_MODIFIER.KEY_SCALE_XY, start_time = 1.2, time = 0.3, end_value = 1.1},
	    {key=EFFECTOR_MODIFIER.KEY_SCALE_XY, start_time = 1.5, time = 0.3, end_value = 1.0},
	    {key=EFFECTOR_MODIFIER.KEY_SCALE_XY, start_time = 1.8, time = 0.3, end_value = 1.1},
	    {key=EFFECTOR_MODIFIER.KEY_SCALE_XY, start_time = 2.1, time = 0.3, end_value = 1.0},
	    {key=EFFECTOR_MODIFIER.KEY_SCALE_XY, start_time = 2.4, time = 0.3, end_value = 1.1},
	    {key=EFFECTOR_MODIFIER.KEY_SCALE_XY, start_time = 2.7, time = 0.3, end_value = 1.0},
    }
    
    
clLocationScreen = {}
-------------------------------------------------------------------------------
setmetatable(clLocationScreen, {__index = clWindow})
-------------------------------------------------------------------------------
function clLocationScreen:pre_init()
    self.zones_moving = {}
    self.apply_tool_zones = {}
    
    self.zoom_zones_table = {}
    self.locations_portals_table = {}
    self.wallpapers_portals_table = {}
    self.actions_table = {}
    self.item_sound_table = {}
    self.crystal = nil
end

function clLocationScreen:reset_states()
    self.zones_moving = {}
    self.apply_tool_zones = {}
    self.global_screen_sound_table = {}
    
    self.zoom_zones_table = {}
    self.locations_portals_table = {}
    self.wallpapers_portals_table = {}
    self.actions_table = {}
    self.item_sound_table = {}
    self.item_cursor_table = {}
    self.crystal = nil
    Tip.tip_text_active = false;
    HintTest.hint_text_active = false;
    self.sound_counters = {}
    clApplication.ignore_lock_obj = nil;
    
    self.mini_game = nil;
    
    Inventory.lock_drag = nil;
    
    clScreenManager:remove_screen_by_name("tutorial"); 
end

function clLocationScreen:AddHiddenCrystal(crystal_name, crystal_on_end)
    self.crystal = {}
    self.crystal.name = crystal_name;
    self.crystal.on_end = crystal_on_end;
end

function clLocationScreen:ZoomZoneLink(_trigger, _zoom_name, _cursor)
    self.zoom_zones_table = self.zoom_zones_table or {}
    _cursor = _cursor or "eye"
    local zoom = {trigger = _trigger, zoom_name = _zoom_name, cursor = _cursor}
    table.insert(self.zoom_zones_table, zoom)
end

function clLocationScreen:ZoomOpen(zoom_name)
    self.zoom = clScreenManager:add_screen(zoom_name) 
    self.zoom.parent = self;
end

function clLocationScreen:CloseZoom()
    if(self.zoom and self.closing_zoom_active == nil)then
        self.closing_zoom_active = true
        self.zoom:FadeIn(0.5);
        DelayedExec(0.7, function()
            self.closing_zoom_active = nil
            clScreenManager:remove_screen(self.zoom);
            self.zoom = nil
        end)
    end
end

function clLocationScreen:ZoomDisable(zoom_name)
    if(self.zoom) then
        SetState(CURRENT_SCREEN, "zoom_"..self.zoom.name, "disable")
    elseif(zoom_name)then
        SetState(CURRENT_SCREEN, "zoom_"..zoom_name, "disable")
    end
    
    self:CloseZoom(); 
end

function clLocationScreen:UnLoadScreen()
    self:CloseZoom(); 
	cpp_application_remove_window(self.name);
	self.win = false;
end
function clLocationScreen:CreateLocationPortal(_trigger, _location, _cursor)
    self.locations_portals_table = self.locations_portals_table or {}
    _cursor = _cursor or "hand"
    local zoom = {trigger = _trigger, location = _location, cursor = _cursor}
    table.insert(self.locations_portals_table, zoom)
end

function clLocationScreen:CreateWallpaperPortal(_trigger, _wallpaper, _pos_x, _pos_y)
    self.wallpapers_portals_table = self.wallpapers_portals_table or {}
    local wallpaper = {trigger = _trigger, name = _wallpaper, pos_x = _pos_x, pos_y = _pos_y}
    table.insert(self.wallpapers_portals_table, wallpaper)
end

function clLocationScreen:ItemSetAction(_item_name, _action)
    self.actions_table = self.actions_table or {}
    local action = {item_name = _item_name, action = _action}
    table.insert(self.actions_table, action)
end

function clLocationScreen:OnKeyDown(key) 
    if(clApplication.game_pause) then return end
    
    if(self.mini_game and self.mini_game.OnKeyDown) then
        self.mini_game:OnKeyDown(key);
    end
end

function clLocationScreen:ShowHint(item_name, pos_x, pos_y) 
    local is_obj = HUDScreen:CreateObj(OBJ_TYPE.ACTIVE, item_name, "graphics/screens/01_ward/location/"..item_name, pos_x, pos_y);    
        
        if(is_obj == true) then
            EffectorPlay(item_hint_eff, item_name, false)
            DelayedExec(0.8, function()
                HUDScreen:RemoveItem(item_name, 0.3)
            end)
        end
end

function clLocationScreen:OnSkip()
    if(self.mini_game and self.mini_game.OnSkip) then
        return self.mini_game:OnSkip();
    end
end

function clLocationScreen:OnHint()
    if(self.mini_game and self.mini_game.OnHint) then
        return self.mini_game:OnHint();
    else
        if(self.hint_table) then
            for key, value in pairs(self.hint_table) do
                if(value.fun and value:fun()) then
                    self:CreateObj(OBJ_TYPE.NO_ACTIVE, value.obj[1], self.textures_path..value.obj[1], value.obj[2], value.obj[3]);  
                    EffectorPlay(item_hint_eff, value.obj[1], false)
                    DelayedExec(3.0, function()
                        self:RemoveItem(value.obj[1], 0.3)
                    end)  
                    if(value.tool) then
                        Inventory:ShowToolHint(value.tool)
                    end
                    return true;
                end
            end
        end
    end
    
    return false
end

function clLocationScreen:OnClick(item_name)
    if(clApplication.game_pause) then return end
    
    if(self.apply_tool_zones and #self.apply_tool_zones > 0) then
        for key, value in pairs(self.apply_tool_zones) do
            if(item_name == value.trigger and Inventory:GetActiveObj() == value.tool_name) then
                value:end_fun();
                Inventory.lock_drag = true;
                return true;
            end
        end 
    end
    
    if(clApplication.lock_cursor and not self:is_ignore_lock_obj(item_name)) then return end
    if(self.onObjectClick and self:onObjectClick(item_name)) then  return true; end
    if(self.mini_game and self.mini_game:OnClick(item_name)) then  return true end
    
   if(self.zoom_zones_table) then
        for key, value in pairs(self.zoom_zones_table) do
            if(item_name == value.trigger and GetState(CURRENT_SCREEN, "zoom_"..value.zoom_name) ~= "disable") then
                self:ZoomOpen(value.zoom_name)
                return;
            end
        end
    end

    if(self.locations_portals_table) then
        for key, value in pairs(self.locations_portals_table) do
            if(item_name == value.trigger) then
                clApplication:LoadLocation(value.location);
                return;
            end
        end
    end
    
    if(self.wallpapers_portals_table) then
        for key, value in pairs(self.wallpapers_portals_table) do
            if(item_name == value.trigger) then
                local wallpaper = clScreenManager:add_screen("wallpaper") 
                wallpaper:ShowWallpaper(self.textures_path..value.name, value.pos_x, value.pos_y)
                return;
            end
        end
    end
    
    if(self.actions_table) then
        for key, value in pairs(self.actions_table) do
            if(item_name == value.item_name) then
                value:action()
                return;
            end
        end
    end
    
    --Объекты к которым прицеплен звук
    if(self.item_sound_table) then
        for key, value in pairs(self.item_sound_table) do
            if(value[1] == item_name) then
                if(type(value[2]) == "table") then
                    local old_value = self.sound_counters[self.item_sound_table];
                    self.sound_counters[self.item_sound_table] = self.sound_counters[self.item_sound_table] or 0
                    self.sound_counters[self.item_sound_table] = self.sound_counters[self.item_sound_table] + 1;
                    if(self.sound_counters[self.item_sound_table] > #value[2]) then
                        self.sound_counters[self.item_sound_table] = 1
                    end
                    if(PlayTableSfx(value[2], value[2][self.sound_counters[self.item_sound_table]]) == false) then
                        self.sound_counters[self.item_sound_table] = old_value;
                    end
                    
                else
                    PlaySfx(value[2]);
                end
                break;
            end
        end
    end
    
    if(self.crystal and self.crystal.name == item_name) then
        self:CrystalFound(item_name, self.crystal.on_end);
        return;
    end
    --[[
    if(self.click_calc == true) then
        if(self:tool_for_found(item_name)) then
            self.click_calc = false;
            SetState(CURRENT_SCREEN, "click_calc", "false")
        elseif(self:is_wrong_item(item_name)) then
            self.click_amount = self.click_amount + 1
            if(self.click_amount >= 3) then
                self:ShowTutorial(5);
                self.click_calc = false;
                SetState(CURRENT_SCREEN, "click_calc", "false")
            end
        end
    end--]]
end

function clLocationScreen:CrystalFound(item_name, on_end)
    if(GetState("GLOBAL", "crystal") == "") then SetState("GLOBAL", "crystal", "0") end
    
    if(on_end) then on_end() end
    SetState("GLOBAL", "crystal", tostring(tonumber(GetState("GLOBAL", "crystal")) + 1))
    SetState(CURRENT_SCREEN, "found_obj", GetState(CURRENT_SCREEN,"found_obj").." "..item_name)
    SetState(CURRENT_SCREEN, "crystal", "found")
    
    SoundPlay("sound/interface/crystal.ogg")
    ItemRemove(self.name, self.crystal.name)
    ItemRemove(self.name, self.crystal.name.."_shadow")
    HUDScreen:CreateObj(OBJ_TYPE.NO_ACTIVE, "crystal_full", "graphics/interface/hud/crystal_full", 512, 384);           
    ItemSetPosition("crystal_full", WINDOW_WIDTH /2 - ItemGetWidth("crystal_full") / 2, WINDOW_HEIGHT / 2 - ItemGetHeight("crystal_full") / 2) 
    ItemAttachChild(HUDScreen:GetName(), "crystal_full")
    EffectorPlay(crystall_found_eff, "crystal_full", false)
    
    HUDScreen:ShowCrystalCounter()
    
    DelayedExec(1.5, function()
        HUDScreen:RemoveItem("crystal_full", 0.3)
    end)
    self.crystal = nil
end

function clLocationScreen:ShowTutorial(_num, _end_fun)
    if(GetState("GLOBAL", "help") == "0" and _num ~= 4) then 
        if(_end_fun) then 
            _end_fun();
        end
        return ;
    end
    
    tutor = clScreenManager:add_screen("tutorial");
    tutor:ShowTutorial(_num)
    tutor.end_fun = _end_fun;
end

function clLocationScreen:SetItemCursor(item_name, cursor_name)
    self.item_cursor_table = self.item_cursor_table or {}
    table.insert(self.item_cursor_table, {item_name, cursor_name})
end

function clLocationScreen:SetItemSound(item_name, sound)
    self.item_sound_table = self.item_sound_table or {}
    table.insert(self.item_sound_table, {item_name, sound})
end

function clLocationScreen:OnMouseMove(item_name)
    if(clApplication.game_pause) then return end
    
    if(self.onMouseMove) then self:onMouseMove(item_name) end
    if(self.mini_game and self.mini_game.OnMouseMove) then self.mini_game:OnMouseMove(item_name) end
    
    if(self.item_cursor_table) then
        for key, value in pairs(self.item_cursor_table) do
            if(item_name == value[1]) then
                clApplication:SetCursor("graphics/interface/cursors/"..value[2])
                return;
            end
        end
    end
    
    if(self.zoom_zones_table) then
        for key, value in pairs(self.zoom_zones_table) do
            if(item_name == value.trigger and GetState(CURRENT_SCREEN, "zoom_"..value.zoom_name) ~= "disable") then
                clApplication:SetCursor("graphics/interface/cursors/"..value.cursor)
                return;
            end
        end
    end

    
    --self.count3 = self.count3 or 1
    --self.count3 = self.count3 + 1
    --SetState("TEST3", "test_", "__"..self.count3)
    
    if(self.locations_portals_table) then
        for key, value in pairs(self.locations_portals_table) do
           --self.count2 = self.count2 or 1
           --self.count2 = self.count2 + 1
 
            --SetState("TEST2", "test_", value.trigger.."__"..self.count2)
            if(item_name == value.trigger) then
                clApplication:SetCursor("graphics/interface/cursors/"..value.cursor)
                return;
            end
        end
    end
    
    
    --self.count = self.count or 1
    --self.count = self.count + 1

    --SetState("TEST", "test_", item_name.."__"..self.count)
    clApplication:SetCursor("graphics/interface/cursors/cursor")
end


function clLocationScreen:AddToolApplyZone(trigger, tool_name, end_fun, return_to_inv)
    self.apply_tool_zones = self.apply_tool_zones or {}
    
    local _apply_zone = {}
    
    _apply_zone.trigger = trigger;
    _apply_zone.tool_name = tool_name;
    _apply_zone.end_fun = end_fun
    _apply_zone.return_to_inv = return_to_inv or true;
    
    table.insert(self.apply_tool_zones, _apply_zone)
end

function clLocationScreen:is_ignore_lock_obj(item_name)
    if(item_name ==  clApplication.ignore_lock_obj) then
        return true
    end
    
    return false
end

function clLocationScreen:__pause_screen_sound()
    for key, value in pairs(self.global_screen_sound_table) do
        cpp_sound_pause(value, true)
    end
end

function clLocationScreen:__unpause_screen_sound()
    for key, value in pairs(self.global_screen_sound_table) do
        cpp_sound_pause(value, false)
    end
end

function clLocationScreen:SoundPlay(sound_name, loop_flag, fade_time)
    local sound_id = SoundPlay(sound_name, loop_flag, fade_time)
    table.insert(self.global_screen_sound_table, sound_id)
    
    return sound_id;
end

function clLocationScreen:Update()
    if(clApplication.game_pause) then return end
    
    if(self.mini_game and self.mini_game.Update) then
        self.mini_game:Update();
    end
end

function clLocationScreen:PauseScreen()
    EffectorPauseGroup(1)
    PauseRequests(0)
    self:__pause_screen_sound();
end

function clLocationScreen:UnPauseScreen()
    EffectorUnPauseGroup(1);
    UnPauseRequests(0);
    self:__unpause_screen_sound();
end


function NewLocation(objects_list, win_name, _script_name)
	objects_list[win_name] = {}
	setmetatable(objects_list[win_name], {__index =  clLocationScreen})
	objects_list[win_name].name = win_name;
	objects_list[win_name].script_name = _script_name;
	objects_list[win_name].win = false;
	GameScreen = objects_list[win_name];
	GameScreen:pre_init();
	require(_script_name);
end
