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.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
    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
                mPresenter?.beginSkill(durationId, data?.id)

            })
        mTimeAdapter = TimeAdapter(this, this)
        mDiscountAdapter = MoreDiscountAdapter(this)
        id_rl_time?.layoutManager = GridLayoutManager(this, 4)
        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()
        //
    }


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


    }

    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) {
                val intent = Intent(this, ToPaySkillGoodsActivity::class.java)
                intent.putExtra("skillgoods", skillGoodsEntity)
                startActivity(intent)
            } else {
                val intent = Intent(this, SkillPayActivity::class.java)
                intent.putExtra("skillgoods", skillGoodsEntity)
                startActivity(intent)
            }
        }
    }

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

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


}