package com.yidianling.ydlcommon.custom.pay.redpacket.jump

import android.app.Activity
import android.content.Context
import android.content.Intent
import com.yidianling.ydlcommon.custom.mvp.MVPActivity
import com.yidianling.ydlcommon.custom.pay.PayActivity
import com.yidianling.ydlcommon.custom.pay.PayParams
import com.yidianling.ydlcommon.custom.pay.redpacket.RedPacketBean
import com.yidianling.ydlcommon.custom.pay.redpacket.RedPacketPayBean

/**
 * @author yuanWai
 * @描述:跳转支付页 中间处理页 （目前只有测评模块调用）
 *       这个页面会先掉红包接口 获取红包列表之后 在跳转支付页面 用来缓解 支付页调用太多接口等待不友好现象
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2018/10/24
 */
class JumpPayActivity : MVPActivity<IJumpPayContract.Presenter>(),
    IJumpPayContract.View {

    private var payParams : PayParams? = null

    companion object {
        private const val PARAM = "param"
        fun start(context: Context,payParams: PayParams) {
            val intent = Intent(context, JumpPayActivity::class.java)
            intent.putExtra(PARAM,payParams)
            context.startActivity(intent)
        }

        fun newIntent(context: Context,payParams: PayParams) : Intent{
            return Intent(context, JumpPayActivity::class.java).putExtra(
                PARAM,payParams)
        }
    }

    override fun layoutResId(): Int {
        return 0
    }

    override fun createPresenter(): IJumpPayContract.Presenter {
        return JumpPayPresenterImpl(this)
    }

    //获取入参
    private fun getParam(){
        if (null == intent){
            finish()
        }
        if (intent.hasExtra(PARAM)){
            payParams = intent.getParcelableExtra(PARAM)
        }
    }

    override fun initDataAndEvent() {
        showProgressDialog()
        getParam()
        // 隐藏ActionBar
        supportActionBar?.hide()
        //请求红包列表接口
        presenter.requestCoupon()
    }

    override fun couponResult(data: ArrayList<RedPacketBean>) {
        dismissProgressDialog()
        if (null == payParams){
            jumpPay(
                RedPacketPayBean(
                    PayParams(),
                    data
                )
            )
        }else{
            jumpPay(RedPacketPayBean(payParams!!, data))
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        setResult(Activity.RESULT_OK)
    }

    override fun onFailed(msg: String?) {
        dismissProgressDialog()
        if (null == payParams){
            jumpPay(
                RedPacketPayBean(
                    PayParams(),
                    ArrayList()
                )
            )
        }else{
            jumpPay(RedPacketPayBean(payParams!!, ArrayList()))
        }
        finish()
    }

    private fun jumpPay(data: RedPacketPayBean) {
        startActivity(PayActivity.newIntent(this,data))
        finish()
    }

    override fun onError(t: Throwable?) {
        dismissProgressDialog()
        if (null == payParams){
            jumpPay(
                RedPacketPayBean(
                    PayParams(),
                    ArrayList()
                )
            )
        }else{
            jumpPay(RedPacketPayBean(payParams!!, ArrayList()))
        }
        finish()
    }
}