package com.dahe.gldriver.ui.waybill.activity

import android.os.Bundle
import android.view.View
import com.dahe.gldriver.R
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.databinding.ActivityWaybillSuccBinding
import com.dahe.gldriver.event.RefreshEvent
import com.dahe.gldriver.ui.HomeActivity
import com.dahe.mylibrary.base.BaseActivity
import com.dahe.mylibrary.utils.ActivityUtils
import com.dahe.mylibrary.utils.AppUtils
import com.dahe.mylibrary.utils.BaseUtils
import com.dahe.mylibrary.utils.TimeUtil
import com.dahe.mylibrary.utils.ToastUtils
import org.greenrobot.eventbus.EventBus

/**
 * @ClassName WaybillSuccActivity
 * @Author john
 * @Date 2024/1/29 16:42
 * @Description 接单 装货 卸货成功页面
 */
class WaybillSuccActivity : BaseActivity<ActivityWaybillSuccBinding>() {
    lateinit var orderId: String
    override fun initView(savedInstanceState: Bundle?) {
        setStatusBarColor(R.color.white)
        val type = intent.extras?.getInt(AppConfig.SUCCESS_TYPE)
        orderId = intent.extras?.getString(AppConfig.ORDER_ID).toString()
        setTitleBar(if (type == 0) "接单成功" else if (type == 1) "装货成功" else "卸货成功", true)
        changeType(type)
        binding.run {
            btnLeft.setOnClickListener {
                goLeftOrRight(type, true)
            }
            btnRight.setOnClickListener {
                goLeftOrRight(type, false)
            }

            tvTime.text = TimeUtil.getNowString("")
        }
    }

    override fun initDate() {

    }

    /**
     * type: 0->接单成功（默认）， 1->装货成功 ， 2->卸货成功
     */
    private fun changeType(type: Int?) {
        when (type) {
            1 -> {
                setTitleBar("装货成功")
                binding.run {
                    ivSucc.layoutParams.run {
                        width = BaseUtils.dip2px(mContext, 220f)
                        height = BaseUtils.dip2px(mContext, 150f)
                        ivSucc.layoutParams = this
                        ivSucc
                    }.setBackgroundResource(R.drawable.icon_succ_load)
                    tvSucc.text = "恭喜您，装货成功"
                    btnLeft.text = "返回首页"
                    btnRight.text = "查看运单"
                }

            }

            2 -> {
                setTitleBar("卸货成功")
                binding.run {
                    ivSucc.layoutParams.run {
                        width = BaseUtils.dip2px(mContext, 220f)
                        height = BaseUtils.dip2px(mContext, 150f)
                        ivSucc.layoutParams = this
                        ivSucc
                    }.setBackgroundResource(R.drawable.icon_succ_unload)
                    tvSucc.text = "恭喜您，卸货成功"
                    btnLeft.text = "返回首页"
                    btnRight.visibility = View.GONE
//                    btnRight.text = "查看运单"
                }
            }
        }
    }

    /*
    *
    * */
    private fun goLeftOrRight(type: Int?, isLeft: Boolean) {
        if (isLeft) {
            when (type) {
                0 -> ActivityUtils.finishToActivityAndGoNew(
                    mContext,
                    HomeActivity::class.java,
                    WaybillLoadActivity::class.java,
                    intent.extras,
                    false
                )

                1, 2 -> {
                    ActivityUtils.finishToActivity(HomeActivity::class.java, false)
                    EventBus.getDefault().post(RefreshEvent(type))
                }
            }
        } else {
            when (type) {
                0 -> ActivityUtils.finishToActivityAndGoNew(
                    mContext,
                    HomeActivity::class.java,
                    WaybillUpPicActivity::class.java,
                    intent.extras,
                    false
                )

                1 -> ActivityUtils.finishToActivityAndGoNew(
                    mContext,
                    HomeActivity::class.java,
                    WaybillUnlLoadActivity::class.java,
                    intent.extras,
                    false
                )

                2 -> ActivityUtils.finishToActivity(HomeActivity::class.java, false)
            }
        }
    }

}