package com.zxhl.main.page.fragment

import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.zxhl.cms.common.base.BaseFragment
import com.zxhl.cms.net.model.shop.ShopListEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.utils.Utils
import com.zxhl.main.R
import com.zxhl.main.page.activity.ShopGoodsDetailActivity
import com.zxhl.main.page.adapter.ShopListAdapter
import com.zxhl.main.page.contract.ShopListContract
import com.zxhl.main.page.presenter.ShopListPresenter
import kotlinx.android.synthetic.main.fragment_refresh_layout2.*

/**
 * @author (wangXuewei)
 * @detail : 商店列表
 */
class ShopListFragment : BaseFragment, ShopListContract.View,
    OnRecycleItemClickListener<ShopListEntity> {
    private var mPresenter: ShopListPresenter? = null
    private var mAdapter: ShopListAdapter? = null
    private val title: String

    constructor(title: String) : super() {
        this.title = title
    }

    constructor() : super() {
        this.title = ""
    }

    override fun layoutID(): Int {
        return R.layout.fragment_refresh_layout2
    }

    override fun init(view: View?) {
        mPresenter = ShopListPresenter(this)
        mAdapter = ShopListAdapter(mActivity, this)

        recyclerView2.layoutManager = GridLayoutManager(mActivity, 2)
        recyclerView2?.adapter = mAdapter
        Utils.setOnScrollBottomListener(recyclerView2) { _, _ ->
            if (mAdapter?.getLoadMore() == true) {
                mPresenter?.getShopList(title, false)
            }
        }
        refreshLayout2?.setColorSchemeColors(resources.getColor(com.zxhl.cms.R.color.color_3A95FD))
        refreshLayout2?.setOnRefreshListener {
            mPresenter?.clearPage()
            mPresenter?.getShopList(title, true)
        }
        mPresenter?.getShopList(title, false)
    }

    override fun lazyLoad() {
        super.lazyLoad()
        if (isInitial && isVisible && mAdapter?.itemCount == 0) {
            mPresenter?.getShopList(title, true)
        }

    }

    override fun setShopListData(boolean: Boolean, reslut: List<ShopListEntity>?) {
        refreshLayout2?.isRefreshing = false
        if (boolean) {
            mAdapter?.clear()
            mAdapter?.appendToList(reslut)
            mAdapter?.notifyDataSetChanged()
        } else {
            val start = mAdapter?.getItemCount() ?: 0
            mAdapter?.appendToList(reslut)
            val end = mAdapter?.getItemCount() ?: 0
            mAdapter?.notifyItemRangeChanged(start, end)
        }
    }

    override fun setLoadMore(isBoolean: Boolean) {
        mAdapter?.setLoadMore(isBoolean)
    }

    companion object {
        fun newInstance(string: String): ShopListFragment {
            return ShopListFragment(string)
        }
    }

    override fun onItemClick(view: View?, position: Int, data: ShopListEntity?) {
        if (activity == null) {
            return
        }
        var intent = Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        intent.putExtra("goods", data)
        intent.setClass(activity!!, ShopGoodsDetailActivity().javaClass)
        activity?.startActivity(intent)
    }


}