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

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.amap.api.maps2d.model.LatLng
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.bean.UpPicBean
import com.dahe.gldriver.bean.WaybillPhoto
import com.dahe.gldriver.databinding.ActivityWaybillUppicBinding
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.gldriver.oss.OssServiceUtil
import com.dahe.gldriver.utils.GDLocationUtils
import com.dahe.mylibrary.adapter.GridImageAdapter
import com.dahe.mylibrary.adapter.GridImageAdapter.OnItemClickListener
import com.dahe.mylibrary.base.BaseActivity
import com.dahe.mylibrary.callback.OnPicResultListener
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.utils.ActivityUtils
import com.dahe.mylibrary.utils.LoadingUtils
import com.dahe.mylibrary.utils.PopsUtils
import com.dahe.mylibrary.utils.ToastUtils
import com.dahe.mylibrary.weight.FullyGridLayoutManager
import com.luck.picture.lib.decoration.GridSpacingItemDecoration
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.utils.DensityUtil
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers

/**
 * @ClassName WaybillUpPicActivity
 * @Author john
 * @Date 2024/1/30 15:56
 * @Description 上传照片：装卸货以及回单照片
 */
class WaybillUpPicActivity : BaseActivity<ActivityWaybillUppicBinding>() {

    lateinit var upBean: UpPicBean//提交对象
    lateinit var picBean: WaybillPhoto//照片对象

    var orderId: String = ""
    var waybillId: String = ""
    var childreId: String = ""
    lateinit var latLng: LatLng

    override fun initView(savedInstanceState: Bundle?) {
        upBean = UpPicBean()


        var type = intent.extras?.run {
            orderId = getString(AppConfig.ORDER_ID, "")
            waybillId = getString(AppConfig.WAYBILL_ID, "")
            childreId = getString(AppConfig.CHILDRE_ID, "")
            getInt(AppConfig.SUCCESS_TYPE, 0)
        }
        if (1 == type) {
            binding.run {
                tvTitle.text = "装货"
                tvPicTitle.text = "装货照片"
                tvRece.text = "装货回单"
            }
        }


        binding.run {
            tvPosi.setOnClickListener {
                getLocation()
            }


            recyclerView.run {
                layoutManager =
                    FullyGridLayoutManager(mContext, 4, GridLayoutManager.VERTICAL, false)
                addItemDecoration(
                    GridSpacingItemDecoration(
                        4,
                        DensityUtil.dip2px(mContext, 8f), false
                    )
                )

                var myAdapter = GridImageAdapter(mContext, mData)

                myAdapter.setOnItemClickListener(object : OnItemClickListener {
                    override fun onItemClick(v: View?, position: Int) {

                    }

                    override fun openPicture() {
                        PopsUtils.getInstance().showSelPic(mContext, myAdapter) {

                            OssServiceUtil.getInstance().run {
                                asyncPutImage(it[0].realPath, this@WaybillUpPicActivity)
                                setResultCallBack { data, oldPath ->
                                    picBean = WaybillPhoto()
                                    picBean.run {
                                        photoUrl = oldPath
                                        this.type = if (type == 1) "1" else "2"
                                    }

                                    upBean.waybillPhotoList.add(picBean)
                                }
                            }
                            myAdapter.data.addAll(it)
                            myAdapter.notifyDataSetChanged()
                        }

                    }
                })
                adapter = myAdapter
            }



            //回单处理
            recyclerView2.run {
                layoutManager =
                    FullyGridLayoutManager(mContext, 4, GridLayoutManager.VERTICAL, false)
                addItemDecoration(
                    GridSpacingItemDecoration(
                        4,
                        DensityUtil.dip2px(mContext, 8f), false
                    )
                )

                var myAdapter = GridImageAdapter(mContext, mData)

                myAdapter.setOnItemClickListener(object : OnItemClickListener {
                    override fun onItemClick(v: View?, position: Int) {

                    }

                    override fun openPicture() {
                        PopsUtils.getInstance().showSelPic(mContext, myAdapter) {

                            OssServiceUtil.getInstance().run {
                                asyncPutImage(it[0].realPath, this@WaybillUpPicActivity)
                                setResultCallBack { data, oldPath ->
                                    picBean = WaybillPhoto()
                                    picBean.run {
                                        photoUrl = oldPath
                                        receiptType = if (type == 1) "4" else if (type == 2) "5" else "6"
                                        this.type = "3"
                                    }
                                    upBean.waybillPhotoList.add(picBean)
                                }
                            }
                            myAdapter.data.addAll(it)
                            myAdapter.notifyDataSetChanged()
                        }

                    }
                })
                adapter = myAdapter
            }



            btnUnload.setOnClickListener {
                upBean.run {
                    orderId = this@WaybillUpPicActivity.orderId
                    waybillId = this@WaybillUpPicActivity.waybillId
                    latitude = latLng.latitude.toString()
                    longitude = latLng.longitude.toString()
                    orderChildreId = this@WaybillUpPicActivity.childreId
                    this.type = if (type == 1) "1" else "2"
                }

                DataManager.getInstance().driverLoadOrUnload(upBean)
                    .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(BaseObserver(mContext, object : RxHttpCallBack<String>() {
                        override fun onSuccess(t: CommonResponseBean<String>) {
                            super.onSuccess(t)
                        }
                    }))



                if (1 == type) {
                    ActivityUtils.startActivity(
                        mContext,
                        WaybillSuccActivity::class.java,
                        Bundle().apply { putInt(AppConfig.SUCCESS_TYPE, 1) })
                } else {
                    ActivityUtils.startActivity(
                        mContext,
                        WaybillSuccActivity::class.java,
                        Bundle().apply { putInt(AppConfig.SUCCESS_TYPE, 2) })
                }
            }
        }
    }


    private val mData: List<LocalMedia> = ArrayList()
    override fun initDate() {
        getLocation()
    }


    fun getLocation() {
        LoadingUtils.instance.showLoading(this,"定位中,请稍等...")
        GDLocationUtils.instance.getLocation(this@WaybillUpPicActivity) {
            //errCode等于0代表定位成功，其他的为定位失败，具体的可以参照官网定位错误码说明
            runOnUiThread {
                LoadingUtils.instance.dissLoading()
                if (it.getErrorCode() == 0) {
                    it.getLongitude()//经    度
                    it.getLatitude()//纬    度
                    latLng = LatLng(it.latitude, it.longitude)
                    binding.tvLoca.text = it.address
                } else {
                    showToast("定位失败,请检查定位权限是否开启")
                }
            }
        }
    }
}