package com.zxbw.modulemain.activity

import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.zxbw.modulemain.R
import com.zxbw.modulemain.adapter.XymhSearchReslutAdapter
import com.zxbw.modulemain.contract.SearchReslutContract
import com.zxbw.modulemain.presenter.SearchResultPresenter
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.net.model.qy.TbGoodsEntity
import com.zxhl.cms.net.model.qy.TbGoodsItemEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.utils.RecyclerViewScrollListener
import kotlinx.android.synthetic.main.activity_layout_search_reslut.*
import kotlinx.android.synthetic.main.layout_search_reslut_empty_view.*


class XymhSearchResultActivity : BaseActivity(), SearchReslutContract.View,
    OnRecycleItemClickListener<TbGoodsItemEntity> {
    private var mPresenter: SearchResultPresenter? = null
    private var mAdapterXymh: XymhSearchReslutAdapter? = null
    private var keyword: String? = ""

    override fun onClick(v: View?) {

    }

    override fun before() {
        super.before()
        setStatusBarBackground(AppContext.get().resources.getColor(R.color.transparent))
    }

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

    override fun init() {
        keyword = intent?.data?.getQueryParameter("keyword") ?: ""
        id_edit_search?.setText(keyword)
        id_img_back?.setOnClickListener {
            finish()
        }
        mPresenter = SearchResultPresenter(this)
        mAdapterXymh = XymhSearchReslutAdapter(this, this)
        id_rl_seach_reslut?.layoutManager = LinearLayoutManager(this)
        id_rl_seach_reslut?.adapter = mAdapterXymh
        id_rl_seach_reslut?.addOnScrollListener(object : RecyclerViewScrollListener() {
            override fun onScrollToBottom() {
                super.onScrollToBottom()
                if ((mAdapterXymh?.list?.size ?: 0) > 0) {
                    mPresenter?.getShopSearchList(false, keyword)
                }
            }
        })
        id_refresh_layout?.setOnRefreshListener {
            mPresenter?.getShopSearchList(true, keyword)
        }
        id_img_clear?.setOnClickListener {
            finish()
        }
        getSearchData()
        id_edit_search?.setOnFocusChangeListener(object : View.OnFocusChangeListener {
            override fun onFocusChange(v: View?, hasFocus: Boolean) {
                if (hasFocus) {

                }
            }

        })
        id_edit_search?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

                keyword = s.toString().trim()

                getSearchData()
            }

            override fun afterTextChanged(s: Editable?) {

            }

        })

    }

    override fun setData(isRefresh: Boolean, result: TbGoodsEntity?) {
        id_ll_empty?.visibility = View.GONE
        if (isRefresh) {
            mAdapterXymh?.clear()
        }
        id_refresh_layout?.isRefreshing = false
        val start = mAdapterXymh?.getItemCount() ?: 0
        mAdapterXymh?.appendToList(result?.result_list)
        if (isRefresh) {
            mAdapterXymh?.notifyDataSetChanged()
        } else {
            val end = mAdapterXymh?.getItemCount() ?: 0
            mAdapterXymh?.notifyItemRangeChanged(start, end)
        }
    }

    override fun setEmptyView() {
        mAdapterXymh?.clear()
        id_ll_empty?.visibility = View.VISIBLE
    }

    fun getSearchData() {
        mPresenter?.getShopSearchList(true, keyword)
    }

    override fun onItemClick(view: View?, position: Int, data: TbGoodsItemEntity?) {
        if (data?.bannerType.equals("blindBox")){
//            if (!Constant.Switch.isOpenVip) {
//                JumpUtils.MemberOrderJump()
//            }else{
//                JumpUtils.goodsDetailJump(data?.boxId)
//            }
        }else{
            var intent = Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            intent.putExtra("goods", data)
            intent.setClass(this, XymhTbGoodsDetailActivity().javaClass)
            startActivity(intent)
        }
    }


}