package com.duben.microtribe.ui.activitys

import android.os.Looper
import android.text.TextUtils
import android.view.View
import androidx.core.content.ContextCompat
import com.duben.microtribe.MintsApplication
import com.duben.microtribe.R
import com.duben.microtribe.mvp.presenters.BackCashPresenter
import com.duben.microtribe.mvp.views.BackCashView
import com.duben.microtribe.ui.activitys.base.BaseActivity
import com.duben.microtribe.utils.SpanUtils
import com.duben.microtribe.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_backcash2.*
import kotlinx.android.synthetic.main.header_layout.*

class Backcash2Activity : BaseActivity(), BackCashView, View.OnClickListener {

    private var backCashReason = ""

    private val backCashPresenter by lazy { BackCashPresenter() }

    override fun getContentViewLayoutID() = R.layout.activity_backcash2

    override fun initViewsAndEvents() {
        backCashPresenter.attachView(this)

        tv_title.text = "申请退款"
        iv_left_icon.visibility = View.VISIBLE
        iv_left_icon.setImageResource(R.mipmap.ic_arrow_back)
        iv_left_icon.setOnClickListener { finish() }

        tv_order_nickname.text = SpanUtils()
            .append("1.您的称呼")
            .setForegroundColor(ContextCompat.getColor(this, R.color.black))
            .create()

        tv_order_mobile.text = SpanUtils()
            .append("* ")
            .setForegroundColor(ContextCompat.getColor(this, R.color.red))
            .append("2.您的联系方式")
            .setForegroundColor(ContextCompat.getColor(this, R.color.black))
            .create()

        tv_order_cause.text = SpanUtils()
            .append("* ")
            .setForegroundColor(ContextCompat.getColor(this, R.color.red))
            .append("3.退款原因")
            .setForegroundColor(ContextCompat.getColor(this, R.color.black))
            .create()

        tv_order_suggest.text = SpanUtils()
            .append("4.建议")
            .setForegroundColor(ContextCompat.getColor(this, R.color.black))
            .create()

        rb_1.setOnClickListener(this)
        rb_2.setOnClickListener(this)
        rb_3.setOnClickListener(this)
        rb_4.setOnClickListener(this)
        btn_submit.setOnClickListener(this)
    }

    override fun isApplyKitKatTranslucency() = false

    override fun reqfundSuc() {
        if (isFinishing) return

        ToastUtil.showLong(MintsApplication.getContext(),"退款发起成功!")
        android.os.Handler(Looper.getMainLooper()).postDelayed({
            if (isFinishing) return@postDelayed

            finish()
        }, 400)
    }

    override fun reqfundFail(msg: String) {
        if (isFinishing) return

        ToastUtil.showLong(MintsApplication.getContext(),msg)
    }

    private fun changeChoice(type: Int) {
        when (type) {
            1 -> {
                rb_1.isChecked = true
                rb_2.isChecked = false
                rb_3.isChecked = false
                rb_4.isChecked = false
            }
            2 -> {
                rb_1.isChecked = false
                rb_2.isChecked = true
                rb_3.isChecked = false
                rb_4.isChecked = false
            }
            3 -> {
                rb_1.isChecked = false
                rb_2.isChecked = false
                rb_3.isChecked = true
                rb_4.isChecked = false
            }
            4 -> {
                rb_1.isChecked = false
                rb_2.isChecked = false
                rb_3.isChecked = false
                rb_4.isChecked = true
            }
        }
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.rb_1 -> {
                backCashReason = rb_1.text.toString()
                changeChoice(1)
            }
            R.id.rb_2 -> {
                backCashReason = rb_2.text.toString()
                changeChoice(2)
            }
            R.id.rb_3 -> {
                backCashReason = rb_3.text.toString()
                changeChoice(3)
            }
            R.id.rb_4 -> {
                backCashReason = rb_4.text.toString()
                changeChoice(4)
            }
            R.id.btn_submit -> {
                if (TextUtils.isEmpty(et_order_mobile.text)) {
                    showToast("联系方式不能为空")
                    return
                }
                if (TextUtils.isEmpty(backCashReason)) {
                    showToast("退款原因不能为空")
                    return
                }
                backCashPresenter.reqfundV1(
                    et_order_mobile.text.toString(),
                    backCashReason,
                    et_order_suggest.text.toString(),
                    et_order_nickname.text.toString()
                )
            }
            else -> {}
        }
    }


}