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

import android.Manifest
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.amap.api.maps2d.AMap
import com.dahe.gldriver.R
import com.dahe.gldriver.adapter.WaybillNodeAdapter
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.bean.OrderDetailBean
import com.dahe.gldriver.databinding.ActivityWaybillLoadBinding
import com.dahe.gldriver.databinding.ActivityWaybillUnloadBinding
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.mylibrary.base.BaseActivity
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.recycleviewswipe.RecycleViewDivider
import com.dahe.mylibrary.utils.ActivityUtils
import com.dahe.mylibrary.utils.BaseUtils
import com.dahe.mylibrary.utils.ConvertUtils
import com.dahe.mylibrary.utils.ToastUtils
import com.permissionx.guolindev.PermissionX
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers

/**
 * @ClassName WaybillLoadActivity
 * @Author john
 * @Date 2024/1/29 16:52
 * @Description 运单详情-卸货
 */
class WaybillUnlLoadActivity : BaseActivity<ActivityWaybillUnloadBinding>() {

    var aMap: AMap? = null


    var orderId: String = ""
    var waybillId: String = ""
    lateinit var orderBean: OrderDetailBean
    override fun initView(savedInstanceState: Bundle?) {

        setTitleBar("运单详情", { finish() }, true, "查看单据") {
            ToastUtils.showToast(mContext, "查看单据")
        }
        orderId = intent.extras?.getString(AppConfig.ORDER_ID, "").toString()
        initRecy()
        binding.run {
            mapView.onCreate(savedInstanceState)
            if (aMap == null) mapView.map

            btnRight.setOnClickListener {
                var data = orderBean.orderChildList.first { it.type == "2" }
                ActivityUtils.startActivity(
                    mContext,
                    WaybillUpPicActivity::class.java,
                    Bundle().apply {
                        putInt(AppConfig.SUCCESS_TYPE, 2)
                        putString(AppConfig.ORDER_ID, orderId)
                        putString(AppConfig.WAYBILL_ID, orderBean.waybillId)
                        putString(AppConfig.CHILDRE_ID, data.orderChildreId)
                    })
            }
        }
    }

    override fun initDate() {

        DataManager.getInstance().orderDetail(orderId)
            .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(BaseObserver(mContext, object : RxHttpCallBack<OrderDetailBean>() {
                override fun onSuccess(t: CommonResponseBean<OrderDetailBean>) {
                    super.onSuccess(t)
                    orderBean = t.data

                    binding.run {
                        tvTime.text = orderBean.receiverDeadline
                        tvType.text = if (orderBean.orderType == "0") "一装一卸" else "一装多卸"

                        tvCom.text = orderBean?.receiverBusinessName
                        tvFreight.text = orderBean?.driverFreight.toString()

                        btnLeft.setOnClickListener {
                            PermissionX.init(this@WaybillUnlLoadActivity)
                                .permissions(Manifest.permission.CALL_PHONE)
                                .request { allGranted, grantedList, deniedList ->
                                    if (allGranted) {
                                        BaseUtils.callPhone(
                                            this@WaybillUnlLoadActivity,
                                            orderBean.shipperContactPhone
                                        )
                                    } else {
                                        Toast.makeText(
                                            mContext,
                                            "开启权限失败,请在应用设置-权限中打开电话权限",
                                            Toast.LENGTH_SHORT
                                        ).show()
                                    }
                                }

                        }
                    }

                    adapter?.submitList(t.data.orderChildList)
//                    initXml(t.data)
                }
            }))
    }


    var adapter: WaybillNodeAdapter? = null
    private fun initRecy() {
        adapter = binding.recycler.run {
            layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
            setHasFixedSize(true)
            addItemDecoration(
                RecycleViewDivider(
                    LinearLayout.VERTICAL,
                    ConvertUtils.dp2px(16.0f),
                    Color.TRANSPARENT
                )
            )
            adapter = WaybillNodeAdapter()
            adapter as WaybillNodeAdapter
        }.apply {
            setOnItemClickListener { _, _, position ->
                ActivityUtils.startActivity(
                    mContext,
                    WaybillLoadActivity::class.java,
                    Bundle().apply { putString(AppConfig.ORDER_ID, items[position].orderId) })
            }
            addOnItemChildClickListener(R.id.btnOk) { adapter, view, position ->
                ActivityUtils.startActivity(mContext, WaybillDetailActivity::class.java)
            }
        }

    }


    override fun onResume() {
        super.onResume()
        binding.mapView.onResume();
    }

    override fun onPause() {
        super.onPause()
        binding.mapView.onPause();
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        binding.mapView.onSaveInstanceState(outState);
    }

    override fun onDestroy() {
        if (binding.mapView != null)
            binding.mapView.onDestroy();
        super.onDestroy()

    }


}