package com.duben.microtribe.ui.activitys

import android.app.Dialog
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.google.gson.JsonObject
import com.duben.microtribe.MintsApplication
import com.duben.microtribe.R
import com.duben.microtribe.manager.AppHttpManager
import com.duben.microtribe.manager.UserManager
import com.duben.microtribe.mvp.model.BaseResponse
import com.duben.microtribe.mvp.model.OrderRecordBean.AgreementDTO
import com.duben.microtribe.ui.activitys.base.BaseActivity
import com.duben.microtribe.ui.widgets.CancelOrderDialog
import com.duben.microtribe.ui.widgets.DialogListener
import com.duben.microtribe.utils.ToastUtil
import com.duben.library.net.neterror.BaseSubscriber
import com.duben.library.net.neterror.Throwable
import com.duben.library.utils.nodoubleclick.AntiShake
import kotlinx.android.synthetic.main.activity_manage_order.*
import kotlinx.android.synthetic.main.header_layout.*

/**
 * 描述：管理自动续费
 */
class ManageOrderActivity : BaseActivity(), View.OnClickListener {

    companion object {
        const val AGREEMENT_DATA = "agreement"
    }

    private var mAgreementDTO: AgreementDTO? = null

    override fun getBundleExtras(extras: Bundle?) {
        super.getBundleExtras(extras)
        extras?.let {
            mAgreementDTO = it.getSerializable(AGREEMENT_DATA) as AgreementDTO?
        }
    }

    override fun getContentViewLayoutID() = R.layout.activity_manage_order

    override fun initViewsAndEvents() {
        tv_title.text = "管理自动续费"
        iv_left_icon.visibility = View.VISIBLE
        iv_left_icon.setImageResource(R.mipmap.ic_arrow_back)
        iv_left_icon.setOnClickListener(this)
        tv_cancel.setOnClickListener(this)

        if (TextUtils.isEmpty(UserManager.getInstance().mobile)) {
            tv_my_account.text = "手机号：未授权"
        } else {
            tv_my_account.text = "手机号：" + UserManager.getInstance().mobile
        }
        tv_my_hint.text = "ID：" + UserManager.getInstance().codeId

        mAgreementDTO?.let {
            tv_date.text = it.nextDay
            tv_coin.text = it.balance
            tv_channel.text = it.payChannel
            tv_1.text = it.topText
        }
    }

    override fun isApplyKitKatTranslucency() = false

    override fun onClick(v: View?) {
        if (AntiShake.check(v?.id)) return

        when (v?.id) {
            R.id.iv_left_icon -> finish()
            R.id.tv_cancel -> {
                showCancelListener()
            }
        }
    }

    private fun showCancelListener() {
        CancelOrderDialog(this, object : DialogListener() {
            override fun onClick(dialog: Dialog?, v: View?) {
                super.onClick(dialog, v)
                dialog?.dismiss()
                unSign()
            }
        }).show()
    }

    private fun unSign() {
        AppHttpManager.getInstance(baseApplication)
            .call(baseApplication.loanService.unSign(),
                object : BaseSubscriber<BaseResponse<JsonObject>>() {
                    override fun onError(e: Throwable) {}

                    override fun onNext(t: BaseResponse<JsonObject>) {
                        if (t.status == 200) {
                            ToastUtil.show(MintsApplication.getContext(), "成功取消自动续费")
                            finish()
                        }
                    }

                })
    }
}