package com.zxbw.modulemain.activity

import android.content.Intent
import android.text.TextUtils
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.zxbw.modulemain.R
import com.zxbw.modulemain.adapter.FlashSaleAdapter
import com.zxbw.modulemain.adapter.MoreDiscountAdapter
import com.zxbw.modulemain.adapter.TimeAdapter
import com.zxbw.modulemain.box.activity.OrderDetailActivity
import com.zxbw.modulemain.contract.FlashSaleContract
import com.zxbw.modulemain.presenter.FlashSalePresenter
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.net.model.qy.SkillEntity
import com.zxhl.cms.net.model.qy.SkillGoodsEntity
import com.zxhl.cms.net.model.qy.ToSkillInfoEntity
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.cms.utils.OnRecycleItemClickListener
import kotlinx.android.synthetic.main.activity_layout_flash_sale.*

class FlashSaleActivity : BaseActivity(), FlashSaleContract.View,
    OnRecycleItemClickListener<SkillEntity> {
    private var mPresenter: FlashSalePresenter? = null
    private var mAdapter: FlashSaleAdapter? = null
    private var mDiscountAdapter: MoreDiscountAdapter? = null
    private var mTimeAdapter: TimeAdapter? = null
    private var durationId: String? = ""
    private var skillGoodsEntity: SkillGoodsEntity? = null
    var mChoosList: MutableList<String> = ArrayList()
    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_flash_sale
    }

    override fun init() {
        id_img_back?.setOnClickListener {
            finish()
        }
        mPresenter = FlashSalePresenter(this)
        mAdapter = FlashSaleAdapter(
            this,
            OnRecycleItemClickListener<SkillGoodsEntity> { view, position, data ->

                skillGoodsEntity = data
                val user = SettingPreference.getUserInfoData()
                if (user.member) {
                    mPresenter?.beginSkill(durationId, data?.id)
                } else {
                    JumpUtils.MemberOrderJump()
//                    val intent = Intent(this, SkillPayActivity::class.java)
//                    intent.putExtra("skillgoods", skillGoodsEntity)
//                    startActivity(intent)
                }


            })
        mTimeAdapter = TimeAdapter(this, this)
        mDiscountAdapter = MoreDiscountAdapter(this)
        id_rl_time?.layoutManager = GridLayoutManager(this, 5)
        id_rl_time?.adapter = mTimeAdapter
        id_rl_more_discount?.layoutManager = GridLayoutManager(this, 3)
        id_rl_more_discount?.adapter = mDiscountAdapter
        id_rl_flash_sale_top?.layoutManager = GridLayoutManager(this, 3)
        id_rl_flash_sale_top?.adapter = mAdapter
        mPresenter?.getFlashSaleData()
        mPresenter?.getMoreList("more")

        //
    }


    override fun setData(result: List<SkillEntity>?) {
        mTimeAdapter?.clear()
        mTimeAdapter?.appendToList(result)
        mTimeAdapter?.notifyDataSetChanged()
        if (!result.isNullOrEmpty()) {
            for (i in result.indices) {
                if (result[i].defaultSelect == 1) {
                    mTimeAdapter?.setPosition(i)
                    durationId = result.get(i)?.id
                    getGoodsList()
                }
//                if (TextUtils.equals(result[i].status, "9")) {
//
//                }
            }
        }


    }

    override fun setSkillListResult(result: List<SkillGoodsEntity>?) {
        mAdapter?.clear()
        mAdapter?.appendToList(result)
        mAdapter?.notifyDataSetChanged()
    }

    override fun toskill(boolean: Boolean, result: ToSkillInfoEntity?) {
        mPresenter?.getSkillGoodsList(durationId)
        // JumpUtils.ToPaySkillJump()

        if (boolean) {
            val user = SettingPreference.getUserInfoData()
            if (user.member) {
                if (TextUtils.equals(skillGoodsEntity?.goodsType, "50")) {
                    val intent = Intent(this, ToPaySkillGoodsActivity::class.java)
                    intent.putExtra("skillgoods", skillGoodsEntity)
                    intent.putExtra("type","1")
                    startActivity(intent)
                } else if (TextUtils.equals(skillGoodsEntity?.goodsType, "51")) {
                    //卡券商品
                    val intent = Intent(mActivity, CardDetailActivity::class.java)
                    intent.putExtra("cardbean", skillGoodsEntity?.card)
                    intent.putExtra("type","1")
                    startActivity(intent)
                } else if (TextUtils.equals(skillGoodsEntity?.goodsType, "99")) {
                    //实物商品
                    mChoosList.clear()
                    mChoosList.add(skillGoodsEntity?.goods?.id.toString())
                    val intent = Intent(mActivity, ShopOrderActivity::class.java)
                    intent.putStringArrayListExtra(
                        "stringList",
                        mChoosList as java.util.ArrayList<String>?
                    )
                    startActivity(intent)
                }

            } else {
                JumpUtils.MemberOrderJump()
//                val intent = Intent(this, SkillPayActivity::class.java)
//                intent.putExtra("skillgoods", skillGoodsEntity)
//                startActivity(intent)
            }
        }
    }

    override fun setMoreResult(result: List<SkillGoodsEntity>?) {
        mDiscountAdapter?.clear()
        mDiscountAdapter?.appendToList(result)
        mDiscountAdapter?.notifyDataSetChanged()
    }


    override fun onItemClick(view: View?, position: Int, data: SkillEntity?) {
        durationId = data?.id
        getGoodsList()
    }

    fun getGoodsList() {
        mPresenter?.getSkillGoodsList(durationId)
    }


}