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.SearchReslutAdapter
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 SearchResultActivity : BaseActivity(), SearchReslutContract.View,
    OnRecycleItemClickListener<TbGoodsItemEntity> {
    private var mPresenter: SearchResultPresenter? = null
    private var mAdapter: SearchReslutAdapter? = 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)
        mAdapter = SearchReslutAdapter(this, this)
        id_rl_seach_reslut?.layoutManager = LinearLayoutManager(this)
        id_rl_seach_reslut?.adapter = mAdapter
        id_rl_seach_reslut?.addOnScrollListener(object : RecyclerViewScrollListener() {
            override fun onScrollToBottom() {
                super.onScrollToBottom()
                if ((mAdapter?.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) {
            mAdapter?.clear()
        }
        id_refresh_layout?.isRefreshing = false
        val start = mAdapter?.getItemCount() ?: 0
        mAdapter?.appendToList(result?.result_list)
        if (isRefresh) {
            mAdapter?.notifyDataSetChanged()
        } else {
            val end = mAdapter?.getItemCount() ?: 0
            mAdapter?.notifyItemRangeChanged(start, end)
        }
    }

    override fun setEmptyView() {
        mAdapter?.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, TbGoodsDetailActivity().javaClass)
            startActivity(intent)
        }
    }


}