package com.zxhl.main.page.fragment

import android.text.TextUtils
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.zxhl.cms.common.Constant
import com.zxhl.cms.common.base.BaseFragment
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.net.model.box.HomeBoxListEntity
import com.zxhl.cms.net.model.other.IsWeChatLoginEntity
import com.zxhl.cms.utils.*
import com.zxhl.cms.widget.IsLoginCallBack
import com.zxhl.main.R
import com.zxhl.main.page.adapter.BoxAdapter
import com.zxhl.main.page.contract.HomeContract
import com.zxhl.main.page.presenter.HomePresenter
import com.zxhl.main.page.view.BoxResultDialog
import kotlinx.android.synthetic.main.fragment_refresh_layout.*
import kotlinx.android.synthetic.main.layout_new_people.*
import kotlinx.android.synthetic.main.layout_top_box.*
import kotlinx.android.synthetic.main.layout_top_head.*

class AllFragment : BaseFragment, HomeContract.View,
    OnRecycleItemClickListener<HomeBoxListEntity>, WeChatLoginSuccessCallBall {
    private var mPresenter: HomePresenter? = null
    private var mAdapter: BoxAdapter? = null
    private var ownCoin: Double? = 0.0
    private var wechatutil: WeChatUtils? = 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_layout
    }

    override fun init(view: View?) {
        //  showNewPeople()
        wechatutil= WeChatUtils(mActivity,this)
        mPresenter = HomePresenter(this)
        mAdapter = BoxAdapter(mActivity, this)
        //拥有的币数量
        ownCoin = SettingPreference.getUserCoinNum().toDoubleOrNull()

        recyclerView.layoutManager = LinearLayoutManager(mActivity)
        recyclerView?.adapter = mAdapter
//        recyclerView?.setNestedScrollingEnabled(true)

        Utils.setOnScrollBottomListener(recyclerView) { _, _ ->
            if (mAdapter?.getLoadMore() == true) {
                mPresenter?.getHomeBoxList(title, false)
            }
        }
        refreshLayout?.setColorSchemeColors(resources.getColor(com.zxhl.cms.R.color.color_3A95FD))
        refreshLayout?.setOnRefreshListener {
//            if (mAdapter != null) {
//                mAdapter?.clear()
//            }
            mPresenter?.clearPage()
            mPresenter?.getHomeBoxList(title, true)
        }
        //mPresenter?.getHomeBoxList("")
        mPresenter?.getHomeBoxList(title, false)

    }

    override fun lazyLoad() {
        super.lazyLoad()
        Log.d("lazyLoad", "isInitial:" + isInitial + "   isVisible:" + isVisible)
        if (isInitial && isVisible && mAdapter?.itemCount == 0) {
            mPresenter?.getHomeBoxList(title, true)
        }

    }


    override fun setHomeData(boolean: Boolean, reslut: List<HomeBoxListEntity>?) {
        refreshLayout?.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)
        }
        if (reslut != null && reslut.size > 0) {
            SettingPreference.setHomeBoxId(reslut?.get(0)?.boxId)
            SettingPreference.setHomeBoxPrice(reslut?.get(0)?.onePriceStr)
        }

    }

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


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

    override fun onItemClick(view: View?, position: Int, data: HomeBoxListEntity?) {
        when (view?.id) {
            R.id.id_ll_item->{
                JumpUtils.goodsDetailJump(data?.boxId)
            }
            R.id.id_tv_one_lottery->{
                Log.e("MXL","点击抽奖")
                wechatutil?.isWechatLogin(object : IsLoginCallBack<IsWeChatLoginEntity> {
                    override fun onResult(result: IsWeChatLoginEntity?) {
                        if(result?.status.equals("0")){
                            JumpUtils.ThirdPardLoginJump()
                        }else{
                            oneLottery(data)
                        }
                    }

                    override fun onError(code: String?, errorMsg: String?) {

                    }

                })

            }
        }

    }

    fun oneLottery(data: HomeBoxListEntity?){
        ownCoin=SettingPreference.getUserCoinNum().toDoubleOrNull()
        if (ownCoin?.compareTo(data?.onePrice!!)!! >= 0) {
            BoxResultDialog.showOneLotteryResult(
                mActivity,
                data?.boxId ?: "",
                data?.onePrice!!,
                data?.fivePrice!!, 0
            )
        }else{
            val chajia = data?.onePrice?.minus(ownCoin!!)
            JumpUtils.payJump(data?.boxId, chajia.toString(), Constant.Key.ONE_LOTTERY)
        }
    }

    override fun loginSuccess(lottery: String?) {

    }
}