local PlayerInfoUI = class("PlayerInfoUI", BaseUI)

local TAG = {
    AVATAR = 1,
    FRAME = 2,
}

function PlayerInfoUI:isFullScreen()
    return  false
end

function PlayerInfoUI:onPressBackspace()
	self:closeUI()
end

function PlayerInfoUI:getPrefabPath()
    return "assets/prefabs/ui/player_info/player_info_ui.prefab"
end

function PlayerInfoUI:ctor()
    self.selectAvatarId = DataManager.PlayerData:getUsingAvatarId()
    self.selectFrameId = DataManager.PlayerData:getUsingFrameId()
end

function PlayerInfoUI:onCover()
end

function PlayerInfoUI:onReshow()
end

function PlayerInfoUI:onClose()
end

function PlayerInfoUI:onLoadRootComplete()
    self.uiMap = self.root:genAllChildren()

    self.txTitle = self.uiMap["player_info_ui.bg.title.tx_title"]
    self.imgAvatar = self.uiMap["player_info_ui.bg.info.avatar.img_avatar"]
    self.imgFrame = self.uiMap["player_info_ui.bg.info.avatar.img_frame"]
    self.expProg = self.uiMap["player_info_ui.bg.info.exp.exp_prog"]
    self.txLevel = self.uiMap["player_info_ui.bg.info.exp.level.tx_level"]
    self.txName = self.uiMap["player_info_ui.bg.info.nickname.tx_name"]
    self.btnRename = self.uiMap["player_info_ui.bg.info.nickname.btn_rename"]
    self.tagAvatar = self.uiMap["player_info_ui.bg.list.tags.tag_avatar"]
    self.selectAvatar = self.uiMap["player_info_ui.bg.list.tags.tag_avatar.select"]
    self.txAvatar = self.uiMap["player_info_ui.bg.list.tags.tag_avatar.tx_avatar"]
    self.txAvatarS = self.uiMap["player_info_ui.bg.list.tags.tag_avatar.select.tx_avatar_s"]
    self.tagFrame = self.uiMap["player_info_ui.bg.list.tags.tag_frame"]
    self.selectFrame = self.uiMap["player_info_ui.bg.list.tags.tag_frame.select"]
    self.txFrame = self.uiMap["player_info_ui.bg.list.tags.tag_frame.tx_frame"]
    self.txFrameS = self.uiMap["player_info_ui.bg.list.tags.tag_frame.select.tx_frame_s"]
    self.scrollrectAvatar = self.uiMap["player_info_ui.bg.list.scrollrect_avatar"]
    self.scrollrectFrame = self.uiMap["player_info_ui.bg.list.scrollrect_frame"]
    self.txDesc = self.uiMap["player_info_ui.bg.tx_desc"]
    self.btnUse = self.uiMap["player_info_ui.bg.btn_use"]
    self.txUse = self.uiMap["player_info_ui.bg.btn_use.tx_use"]
    self.btnClose = self.uiMap["player_info_ui.bg.btn_close"]
    self.bgAvatar = self.uiMap["player_info_ui.bg.info.avatar"]

    self.curTag = TAG.AVATAR

    self.tagAvatar:addClickListener(function()
        self:refreshScrollrect(TAG.AVATAR)
	end)
    self.tagFrame:addClickListener(function()
        self:refreshScrollrect(TAG.FRAME)
	end)
    self.btnRename:addClickListener(function()
        UIManager:showUI("app/ui/player_info/rename_ui")
        self:closeUI()
	end)
    self.btnUse:addClickListener(function()
        if self.curTag == TAG.AVATAR then
            ModuleManager.PlayerManager:reqChangeAvatar(self.selectAvatarId)
        elseif self.curTag == TAG.FRAME then
            ModuleManager.PlayerManager:reqChangeFrame(self.selectFrameId)
        end
	end)
    self.btnClose:addClickListener(function()
        self:closeUI()
	end)
    self:addEventListener(EventManager.CUSTOM_EVENT.PLAYER_INFO_CLICK_AVATAR, function(id)
        if self.selectAvatarId == id then
            return
        end
        if DataManager.PlayerData:isNewAvatar(id) then
            DataManager.PlayerData:setNewAvatarStatus(id, false)
        end
        self.selectAvatarId = id
        self.scrollRectCompAvatar:updateAllCell()
        self:refreshDesc()
        self:refreshAvatarAndFrame()
    end)
    self:addEventListener(EventManager.CUSTOM_EVENT.PLAYER_INFO_CLICK_FRAME, function(id)
        if self.selectFrameId == id then
            return
        end
        if DataManager.PlayerData:isNewFrame(id) then
            DataManager.PlayerData:setNewFrameStatus(id, false)
        end
        self.selectFrameId = id
        self.scrollRectCompFrame:updateAllCell()
        self:refreshDesc()
        self:refreshAvatarAndFrame()
    end)
    self:bind(DataManager.PlayerData, "dirty", function()
		self:onRefresh()
	end)
end

function PlayerInfoUI:onRefresh()
	self:refreshScrollrect()
    self:refreshAvatarAndFrame()

    self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.PLAYER_INFO))
    local lv = DataManager.PlayerData:getLv()
    local exp = DataManager.PlayerData:getExpPercent()
    self.txLevel:setText(GConst.INT_TO_STRING[lv] or tostring(lv))
    self.expProg:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = exp
    self.txName:setText(DataManager.PlayerData:getNickname())
    self.txAvatar:setText(I18N:getGlobalText(I18N.GlobalConst.AVATAR_HEAD_DESC))
    self.txAvatarS:setText(I18N:getGlobalText(I18N.GlobalConst.AVATAR_HEAD_DESC))
    self.txFrame:setText(I18N:getGlobalText(I18N.GlobalConst.AVATAR_HEAD_FRAME_DESC))
    self.txFrameS:setText(I18N:getGlobalText(I18N.GlobalConst.AVATAR_HEAD_FRAME_DESC))

    -- 红点
    if DataManager.PlayerData:hasNewAvatar() then
        self.tagAvatar:addRedPoint(120, 20, 0.6)
    else
        self.tagAvatar:removeRedPoint()
    end
    if DataManager.PlayerData:hasNewFrame() then
        self.tagFrame:addRedPoint(120, 20, 0.6)
    else
        self.tagFrame:removeRedPoint()
    end
end

function PlayerInfoUI:refreshScrollrect(selectTag)
    if selectTag ~= nil then
        self.curTag = selectTag
    end

    if self.curTag == TAG.AVATAR then
        self:showAvatar()
    elseif self.curTag == TAG.FRAME then
        self:showFrame()
    end
end

-- 显示头像列表
function PlayerInfoUI:showAvatar()
    self.ids = DataManager.PlayerData:getShowAvatarIds()

    self.selectAvatar:setActive(true)
    self.scrollrectAvatar:setActive(true)
    self.selectFrame:setActive(false)
    self.scrollrectFrame:setActive(false)

    if self.scrollRectCompAvatar == nil then
        self.scrollRectCompAvatar = self.scrollrectAvatar:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
        self.scrollRectCompAvatar:addInitCallback(function()
            return "app/ui/player_info/avatar_cell"
        end)
        self.scrollRectCompAvatar:addRefreshCallback(function(index, cell)
            cell:refresh(self.ids[index], self.ids[index] == self.selectAvatarId)
        end)
        self.scrollRectCompAvatar:clearCells()
        self.scrollRectCompAvatar:refillCells(#self.ids)
    else
        self.scrollRectCompAvatar:updateAllCell()
    end

    self:refreshDesc()
end

-- 显示框列表
function PlayerInfoUI:showFrame()
    self.ids = DataManager.PlayerData:getShowFrameIds()

    self.selectAvatar:setActive(false)
    self.scrollrectAvatar:setActive(false)
    self.selectFrame:setActive(true)
    self.scrollrectFrame:setActive(true)

    if self.scrollRectCompFrame == nil then
        self.scrollRectCompFrame = self.scrollrectFrame:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
        self.scrollRectCompFrame:addInitCallback(function()
            return "app/ui/player_info/frame_cell"
        end)
        self.scrollRectCompFrame:addRefreshCallback(function(index, cell)
            cell:refresh(self.ids[index], self.ids[index] == self.selectFrameId)
        end)
        self.scrollRectCompFrame:clearCells()
        self.scrollRectCompFrame:refillCells(#self.ids)
    else
        self.scrollRectCompFrame:updateAllCell()
    end

    self:refreshDesc()
end

-- 刷新头像和头像框
function PlayerInfoUI:refreshAvatarAndFrame()
    local smallFrame = DataManager.PlayerData:getAvatarBg(self.selectAvatarId)
    if smallFrame then
        self.bgAvatar:setSprite(GConst.ATLAS_PATH.ICON_HERO, smallFrame)
    end
    local avatarName = DataManager.PlayerData:getAvatarIconId(self.selectAvatarId)
    if avatarName then
        self.imgAvatar:setSprite(GConst.ATLAS_PATH.ICON_HERO, avatarName)
    end
    local frameName = DataManager.PlayerData:getFrameIconId(self.selectFrameId)
    if frameName then
        self.imgFrame:setSprite(GConst.ATLAS_PATH.ICON_AVATAR, frameName)
    end
end

-- 刷新描述
function PlayerInfoUI:refreshDesc()
    if self.curTag == TAG.AVATAR then
        self.txDesc:setText(DataManager.PlayerData:getAvatarDesc(self.selectAvatarId))
        if DataManager.PlayerData:isAvatarUnlock(self.selectAvatarId) and DataManager.PlayerData:getUsingAvatarId() ~= self.selectAvatarId then
            -- 已解锁，未使用
            self.btnUse:setTouchEnable(true)
            self.btnUse:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_green_1")
            self.txUse:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_8))
        else
            self.btnUse:setTouchEnable(false)
            self.btnUse:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_1")
            if DataManager.PlayerData:getUsingAvatarId() == self.selectAvatarId then
                -- 已解锁，已使用
                self.txUse:setText(I18N:getGlobalText(I18N.GlobalConst.ON_USING_DESC))
            else
                -- 未解锁
                self.txUse:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_7))
            end
        end
    elseif self.curTag == TAG.FRAME then
        self.txDesc:setText(DataManager.PlayerData:getFrameDesc(self.selectFrameId))
        if DataManager.PlayerData:isFrameUnlock(self.selectFrameId) and DataManager.PlayerData:getUsingFrameId() ~= self.selectFrameId then
            -- 已解锁，未使用
            self.btnUse:setTouchEnable(true)
            self.btnUse:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_green_1")
            self.txUse:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_8))
        else
            self.btnUse:setTouchEnable(false)
            self.btnUse:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_1")
            if DataManager.PlayerData:getUsingFrameId() == self.selectFrameId then
                -- 已解锁，已使用
                self.txUse:setText(I18N:getGlobalText(I18N.GlobalConst.ON_USING_DESC))
            else
                -- 未解锁
                self.txUse:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_7))
            end
        end
    end
end

return PlayerInfoUI