ProfileScreen = {}
ProfileScreen.mDisplay = level_mgr:get_layer("ProfileScreen")

ProfileScreen.activeSlots = {}
ProfileScreen.addSlots = {}
ProfileScreen.inactiveSlots = {}

local month_names =
{
    'ids_jan', 
    'ids_feb', 
    'ids_mar', 
    'ids_apr', 
    'ids_may', 
    'ids_jun', 
    'ids_jul', 
    'ids_avg', 
    'ids_sep', 
    'ids_oct', 
    'ids_nov', 
    'ids_dec'
}

----------------------------------------------------------------------------------------------------
function ProfileScreen:onShow()
    Panel:show(false)
    Panel:showTimer(false)

    play_clip(ProfileScreen.mDisplay:get_element('buttons'):getCharacter(), {frame_begin = progress, frame_end = progress})

    ProfileScreen:reset()

    local add_pos = 1
    for i = 1, #ProfileList.data.profiles do
        local prof_rec = ProfileList.data.profiles[i]
        local slot_rec = ProfileScreen.activeSlots[i]
        slot_rec.elem:setVisible(true)

        --slot_rec.bar 
        slot_rec.name_lbl:setUnicodeSumbols(prof_rec.name)
        local progress = math.ceil(((prof_rec.level - 1) / 27) * 100)
        slot_rec.progress_lbl:setText(progress..'%')
        play_clip(slot_rec.bar:getCharacter(), {frame_begin = progress, frame_end = progress})

        setDateText(slot_rec.date_lbl, prof_rec.time['day'], month_names[prof_rec.time['mount']], prof_rec.time['year'])
        slot_rec.time_lbl:setText(string.format('%02d', prof_rec.time['hour'])..':'..string.format('%02d', prof_rec.time['minute']))

        ProfileScreen.addSlots[i].elem:setVisible(false)
        ProfileScreen.inactiveSlots[i].elem:setVisible(false)
        add_pos = i + 1
    end

    if add_pos <= 5 then
        ProfileScreen.addSlots[add_pos].elem:setVisible(true)
        ProfileScreen.inactiveSlots[add_pos].elem:setVisible(false)
    end

end


----------------------------------------------------------------------------------------------------
function ProfileScreen:onHide()
    --Map.mDisplay:stop_amb_effects()
end

----------------------------------------------------------------------------------------------------
function ProfileScreen:ON_SHOW()
	--mouse:setCursorFrame("default")
end


----------------------------------------------------------------------------------------------------
function ProfileScreen_UPDATE()
	return 0
end

----------------------------------------------------------------------------------------------------
function ProfileScreen:reset()
    for i = 1, 5 do
        ProfileScreen.activeSlots[i].elem:setVisible(false)
        ProfileScreen.addSlots[i].elem:setVisible(i == 1)
        ProfileScreen.inactiveSlots[i].elem:setVisible(i ~= 1)
    end
    
end

----------------------------------------------------------------------------------------------------
function ProfileScreen:onClick(aSlot)
    ProfileList:switchProfile(aSlot)
    level_mgr:switch_to_level('MainMenu')
    fade_panel:fade_out()
    --trace('profile'..aSlot..' click')
end

----------------------------------------------------------------------------------------------------
function ProfileScreen:onDelete(aSlot)
    QuitConfirm:show({del_slot = aSlot})
    --ProfileList:delete(aSlot)
    --ProfileScreen:onShow()
    --level_mgr:switch_to_level('MainMenu')
    --fade_panel:fade_out()
    --trace('profile'..aSlot..' delete')
end

----------------------------------------------------------------------------------------------------
function ProfileScreen:onAdd(aSlot)
    --[[local func = function(name)
        ProfileList:add(name)
        level_mgr:switch_to_level('Map')
        fade_panel:fade_out()
    end]]
    NameInput:show(onNameEnter)
end

----------------------------------------------------------------------------------------------------
function ProfileScreen:initActive(aSlot)
    local slotRecord = {}
    local elem = ProfileScreen.mDisplay:get_element('active'..aSlot)

    slotRecord.elem = elem
    slotRecord.select_btn = ProfileScreen.mDisplay:get_element_child(elem, 'active_btn', true)
    slotRecord.delete_btn = ProfileScreen.mDisplay:get_element_child(elem, 'delete_btn', true)
    slotRecord.bar = ProfileScreen.mDisplay:get_element_child(elem, 'bar', true)
    slotRecord.name_lbl = ProfileScreen.mDisplay:get_element_child(elem, 'name', true)
    slotRecord.progress_lbl = ProfileScreen.mDisplay:get_element_child(elem, 'progress', true)
    slotRecord.date_lbl = ProfileScreen.mDisplay:get_element_child(elem, 'date', true)
    slotRecord.time_lbl = ProfileScreen.mDisplay:get_element_child(elem, 'time', true)
    
    local select_click = ProfileScreen.mDisplay:get_element_child(slotRecord.select_btn, 'click')

    ProfileScreen.mDisplay:_set_element_eh(select_click,
        function() if not NameInput.open then ProfileScreen:onClick(aSlot) end end)

    ProfileScreen.mDisplay:_set_element_eh(select_click,
        function()
            if not NameInput.open then 
                play_clip(slotRecord.select_btn:getCharacter(), {frame_begin = 1, frame_end = 1})
            end
        end, {eh_type = eiRollOver})
        
    ProfileScreen.mDisplay:_set_element_eh(select_click,
        function()
            if not NameInput.open then 
                play_clip(slotRecord.select_btn:getCharacter(), {frame_begin = 0, frame_end = 0})
            end
        end, {eh_type = eiRollOut})

    local delete_click = ProfileScreen.mDisplay:get_element_child(slotRecord.delete_btn, 'click')

    ProfileScreen.mDisplay:_set_element_eh(delete_click,
        function() if not NameInput.open then ProfileScreen:onDelete(aSlot) end end)

    ProfileScreen.mDisplay:_set_element_eh(delete_click,
        function()
            if not NameInput.open then 
                play_clip(slotRecord.delete_btn:getCharacter(), {frame_begin = 1, frame_end = 1})
            end
        end, {eh_type = eiRollOver})
        
    ProfileScreen.mDisplay:_set_element_eh(delete_click,
        function()
            if not NameInput.open then 
                play_clip(slotRecord.delete_btn:getCharacter(), {frame_begin = 0, frame_end = 0})
            end
        end, {eh_type = eiRollOut})

    elem:setVisible(false)

    ProfileScreen.activeSlots[aSlot] = slotRecord
end

----------------------------------------------------------------------------------------------------
function ProfileScreen:initAdd(aSlot)
    local slotRecord = {}
    local elem = ProfileScreen.mDisplay:get_element('add'..aSlot)

    slotRecord.elem = elem
    local click = ProfileScreen.mDisplay:get_element_child(elem, 'click')

    ProfileScreen.mDisplay:_set_element_eh(click,
        function() if not NameInput.open then ProfileScreen:onAdd(aSlot) end end)

    ProfileScreen.mDisplay:_set_element_eh(click,
        function() if not NameInput.open then play_clip(elem:getCharacter(), {frame_begin = 1, frame_end = 1}) end end, {eh_type = eiRollOver})
        
    ProfileScreen.mDisplay:_set_element_eh(click,
        function() if not NameInput.open then play_clip(elem:getCharacter(), {frame_begin = 0, frame_end = 0}) end end, {eh_type = eiRollOut})

    elem:setVisible(false)
    ProfileScreen.addSlots[aSlot] = slotRecord
end

----------------------------------------------------------------------------------------------------
function ProfileScreen_INIT()
    get_sys_time()
    for i = 1, 5 do
        ProfileScreen:initActive(i)
        ProfileScreen:initAdd(i)

        local inactive = ProfileScreen.mDisplay:get_element('inactive'..i)
        ProfileScreen.inactiveSlots[i] = {elem = inactive};
        inactive:setVisible(false)
    end

    --[[Map.mDisplay:set_element_eh('btn_menu_view',
        function()
            Bubble:hide(false)
            level_mgr:switch_to_level('MainMenu')
            fade_panel:fade_out()
        end)]]
end