---
--- Generated by MLN Team (http://www.immomo.com)
--- Created by MLN Team.
--- DateTime: 2019-09-05 12:05
---

local _class = {
    _name = 'HomePagerView',
    _version = '1.0'
}

---@public
function _class:new()
    local o = {}
    setmetatable(o, {__index = self})
    return o
end

---@public
---@return View containerView
function _class:rootView()
    if self.containerView then
        return self.containerView
    end
    self.containerView = LinearLayout(LinearType.VERTICAL)
    self.containerView:width(MeasurementType.MATCH_PARENT):height(MeasurementType.MATCH_PARENT)
    self:setupTabSegmentView()
    self:setupViewPager()
    return self.containerView
end

---创建tagSegment
---@private
function _class:setupTabSegmentView()
    local titles = Array()
    titles:add("关注")
    titles:add("推荐")

    --self.placeHolderView = View():bgColor(Color(255, 255, 255, 0.0)):width(window:width()):height(0.5)
    --self.containerView:addView(self.placeHolderView)
    self.tabSegment = TabSegmentView(Rect(0, 0, window:width(),_NaviBarHeight), titles, ColorConstants.Black)
    self.tabSegment:selectScale(1.0):width(MeasurementType.MATCH_PARENT):setAlignment(TabSegmentAlignment.CENTER):setGravity(Gravity.CENTER_HORIZONTAL)
    self.tabSegment:setAlignment(TabSegmentAlignment.CENTER)
    self.containerView:addView(self.tabSegment)
end

---创建滚动视图
---@private
function _class:setupViewPager()
    local top = self.tabSegment:marginTop() + self.tabSegment:height()
    local statusBarHeight = window:stateBarHeight()

    self.adapter = ViewPagerAdapter()
    self.adapter:getCount(function(_)
        return 2
    end)

    self.adapter:initCell(function(cell, pos)
        local tableViewClass = require('MMLuaKitGallery.HomeTableView')
        local tableView = tableViewClass:new()
        cell._tableView = tableView --需持有tableView，否则会被GC
        if pos == 1 then
            cell._tableView:updateCategoryId(1)
            cell.contentView:addView(tableView:tableView(tableView.TYPE_FOLLOW))
        else
            cell._tableView:updateCategoryId(4)
            cell.contentView:addView(tableView:tableView(tableView.TYPE_RECOMMEND))
        end
    end)

    self.adapter:fillCellData(function(cell, pos)
        if pos == 1 then
            cell._tableView:updateCategoryId(1)
        else
            cell._tableView:updateCategoryId(4)
        end
    end)

    self.viewPager = ViewPager()
    self.viewPager:scrollToPage(1,false)
    self.viewPager:width(window:width()):height(MeasurementType.MATCH_PARENT):setGravity(Gravity.CENTER_HORIZONTAL)
    self.viewPager:showIndicator(false)
    self.viewPager:adapter(self.adapter)
    self.tabSegment:relatedToViewPager(self.viewPager, true)
    self.containerView:addView(self.viewPager)
end

return _class