package com.zj365.dc.activity.shop

import android.content.Intent
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.tamsiree.rxkit.RxActivityTool
import com.xty.base.act.BaseTenCentWebViewActivity
import com.xty.base.act.BaseVmAct
import com.xty.network.model.ChooseAddressBean
import com.xty.base.model.ChooseCouponBean
import com.xty.base.model.CreateOrderBean
import com.xty.base.utils.PayUtils
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.event.ReChangeEvent
import com.xty.common.setImage
import com.zj365.dc.BuildConfig
import com.zj365.dc.databinding.ActCreateGoodsOrderBinding
import com.zj365.dc.vm.ShopMallVm
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Route(path = ARouterUrl.CREATE_SHOP_ORDER_ACT)
class CreateGoodsOrderAct : BaseVmAct<ShopMallVm>() {
    val binding by lazy { ActCreateGoodsOrderBinding.inflate(layoutInflater) }

    var bean:CreateOrderBean?= null

    var couponsId:Long? = null
    var addrId:Long? = null

    var bottomSheetDialog: BottomSheetDialog? = null

    var hasCoup: Int = 2

    companion object{
        const val REQUEST_ADDRESS = 0x000011
        const val REQUEST_COUP = 0x000012
    }
    override fun liveObserver() {

        mViewModel.createOrderLiveData.observe(this){
            PayUtils(this).payForGoods(it.data.pay_price.toFloat(),it.data.order_no)
        }

        mViewModel.hasCouponLiveData.observe(this){
            hasCoup = it.data
            if (it.data == 2){ //1有 2无
                binding.tvDiscounts.text = "暂无优惠券"
            }else{
                binding.tvDiscounts.text = "可用优惠券"

            }
        }

        mViewModel.defaultAddressLiveData.observe(this){
            addrId = it.data.id
            binding.tvChooseAddress.text = "${it.data.province}${it.data.city}${it.data.district}${it.data.address}"
        }

    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "创建订单"
        binding.title.mIvBack.setOnClickListener {
            finish()
        }

        bean = intent.getParcelableExtra("bean")
        onClickedListener()
        bean?.let {
            refreshUI(it)
        }
    }

    fun refreshUI(bean: CreateOrderBean){
        binding.imgGoods.setImage(this,bean.pic)
        binding.tvGoodsName.text = bean.prod_name
        binding.tvSpec.text = bean.skuName
        binding.tvNum.text = "数量：${bean.skuNum}"
        binding.tvPrice.text = "商品原价：¥ ${bean.ori_price}"
        binding.tvTotalPrice.text ="¥ ${bean.skuPrice * bean.skuNum}"
        binding.tvPlatformDiscounts.text = "${(bean.ori_price * bean.skuNum) - (bean.skuPrice * bean.skuNum)}"

        if (bean.delivery_mode == 0){ //0,无需发货，1，需要发货
            binding.rxAddress.visibility = View.GONE
        }else{
            binding.rxAddress.visibility = View.VISIBLE
            mViewModel.getDefaultAddress()
        }

        mViewModel.hasCoupon()


    }

    fun onClickedListener(){
        binding.tvPay.setOnClickListener {
            bean?.let {
                mViewModel.createOrder(it.skuId.toString(),it.skuNum,addrId,couponsId)
            }
        }

        binding.tvChooseAddress.setOnClickListener {
            bundle.clear()
            bundle.putString(
                "web_site_url",
                "${BuildConfig.h5url}pages/index/index?pageId=address&type=order"
            )
            RouteManager.goActRequest(ARouterUrl.H5_ACT, this@CreateGoodsOrderAct,REQUEST_ADDRESS,bundle)

        }

        binding.tvDiscounts.setOnClickListener {
            if(hasCoup == 1){
                bundle.clear()
                bundle.putString(
                    "web_site_url",
                    "${BuildConfig.h5url}pages/index/index?pageId=coupon&type=order"
                )
                RouteManager.goActRequest(ARouterUrl.H5_ACT, this@CreateGoodsOrderAct,REQUEST_COUP,bundle)
            }

        }
    }


    override fun setLayout()=binding.root

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun paySuccess(event: ReChangeEvent){
        RouteManager.goAct(ARouterUrl.SHOP_ORDER_LIST_ACT)
        RxActivityTool.finishActivity(BaseTenCentWebViewActivity::class.java)
        finish()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(resultCode == RESULT_OK){
            when(requestCode){
                REQUEST_ADDRESS ->{
                    var addressBean = data?.extras!!.getParcelable<ChooseAddressBean>("address")
                    if (addressBean != null) {
                        addrId = addressBean.id
                        binding.tvChooseAddress.text = "${addressBean.province}${addressBean.city}${addressBean.district}${addressBean.address}"
                    }

                }

                REQUEST_COUP ->{
                    var couponBean = data?.extras!!.getParcelable<ChooseCouponBean>("coupon")
                    if (couponBean != null) {
                        couponsId = couponBean.id
                        binding.tvDiscounts.text = "可优惠${couponBean.as_amount}"
                        bean?.let {
                            binding.tvPlatformDiscounts.text = "${(it.ori_price * bean!!.skuNum) - (it.skuPrice * it.skuNum) - couponBean.as_amount}"

                        }
                    }
                }
            }
        }
    }

}