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

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.dahe.gldriver.R
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.bean.Resources
import com.dahe.gldriver.bean.UpRate
import com.dahe.gldriver.bean.WaybillPhoto
import com.dahe.gldriver.databinding.ActivityWaybillRateBinding
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.OcrUtils
import com.dahe.mylibrary.adapter.GridImageAdapter
import com.dahe.mylibrary.base.BaseActivity
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.utils.ImageLoader
import com.dahe.mylibrary.utils.PopsUtils
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 WaybillRateActivtiy
 * @Author john
 * @Date 2024/3/14 16:36
 * @Description TODO
 */
class WaybillRateActivity : BaseActivity<ActivityWaybillRateBinding>() {

    private var upRate = UpRate()

    override fun initView(savedInstanceState: Bundle?) {
        setStatusBarColor(R.color.white)
        setTitleBar("评价", true)
        binding.run {
            rating.setOnRatingChangeListener { ratingBar, rating ->
                upRate.opinionsStarRating = (rating.toInt() * 2).toString()
            }
            btnOk.setOnClickListener {
                subMint()
            }
            ivAdd.setOnClickListener {
                OcrUtils.getInstance()
                    .noOcrUpPic(mContext, this@WaybillRateActivity) { picPath, ocrResult ->
                        ImageLoader.getInstance()
                            .loadRoundImage(mContext, picPath.locPic, 12, ivAdd)
                        upRate.resourcesList.add(0, Resources().apply {
                            resourcesUrl = picPath.picPath
                            resourcesType = "0"
                        })

                    }

            }
        }
    }

    override fun initDate() {

    }

    private fun subMint() {
        var orderID = intent.extras?.getString(AppConfig.ORDER_ID,"")
        if (upRate.opinionsStarRating.isNullOrEmpty() ||
            binding.etContent.text.isNullOrEmpty() ||
            orderID.isNullOrEmpty() ||
            upRate.resourcesList.size == 0
        ) {
            showToast("请完善信息")
            return
        }
        upRate.opinionsContent = binding.etContent.text.trim().toString()
        upRate.orderId = orderID
        DataManager.getInstance().submitComments(upRate)
            .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(BaseObserver(mContext,object : RxHttpCallBack<Any>(){
                override fun onSuccess(t: CommonResponseBean<Any>) {
                    super.onSuccess(t)
                    finish()
                }
            }))
    }
}