package com.zj365.health.act

import android.content.Intent
import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.xty.base.act.BaseVmAct
import com.xty.common.arouter.ARouterUrl
import com.xty.common.picture.PictureUtils
import com.zj365.health.databinding.ActUploadCheckBinding
import com.zj365.health.vm.PayCancerVm
import com.luck.picture.lib.PictureSelector
import com.xty.common.arouter.RouteManager
import com.xty.common.util.CommonToastUtils
import com.zj365.health.R
import com.zj365.health.adapter.PictureAdapter

/**
 */
@Route(path = ARouterUrl.UPLOAD_CHECK)
class UploadCheckAct : BaseVmAct<PayCancerVm>() {
    var signPath = ""
    val IMAGE_PICKER = 1001
    val mAdapter by lazy { PictureAdapter() }
    val binding by lazy { ActUploadCheckBinding.inflate(layoutInflater) }

    override fun setLayout() = binding.root
    var orderNo: String? = ""
    override fun initData() {
        super.initData()
        orderNo = intent.getStringExtra("orderNo")
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "上传检验单"
        binding.title.mIvBack.setOnClickListener { finish() }
//        binding.ivUpload.setOnClickListener {
//            PictureUtils.openCamera(this, PictureMimeType.ofImage(),5)
//        }

        binding.title.mTvRight.apply {
            visibility = View.VISIBLE
            text = "预约采血"
            setTextColor(resources.getColor(R.color.col_02c))
            setOnClickListener {
                bundle.clear()
                RouteManager.goAct(ARouterUrl.COLLECT_BLOOD_list, bundle)
            }
        }
        binding.getBlood.setOnClickListener {
            bundle.clear()
            RouteManager.goAct(ARouterUrl.COLLECT_BLOOD, bundle)
        }
        binding.rvRecyclerview.layoutManager = GridLayoutManager(this, 4)
        binding.rvRecyclerview.adapter = mAdapter
        mAdapter.addChildClickViewIds(R.id.ivImage, R.id.ivDelete)
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.ivImage -> {
                    val item = adapter.getItem(position) as String
                    if (TextUtils.isEmpty(item)) {
                        val value = 5 - (mAdapter.data.size - 1)
                        PictureUtils.openCamera(this, PictureMimeType.ofImage(), value)
                    } else {

                    }
                }
                R.id.ivDelete -> {
                    adapter.removeAt(position)
                    adapter.notifyItemRemoved(position)
                }
            }

        }
        mAdapter.addData("")
        binding.mSubmit.setOnClickListener {
            result.clear()
            for (datum in mAdapter.data) {
                if (TextUtils.isEmpty(datum)) {
                    continue
                }
                result.add(datum)
            }
            if (result.isEmpty()) {
                CommonToastUtils.showToast("请上传检验单")
                return@setOnClickListener
            }
            mViewModel.uploadImagesFile(result)
        }
        mViewModel.isCanReservation()
    }

    var result = mutableListOf<String>()
    var fileType = 0 //当前发送的文件类型 1 照片 3 视频
    var fileUrl = "" //当前选择的文件的路径
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (resultCode === RESULT_OK) {
            when (requestCode) {
                PictureConfig.CHOOSE_REQUEST -> {
                    var result = PictureSelector.obtainMultipleResult(data)
                    if (result.size > 0) {
                        if (result.isNotEmpty()) {
                            fileUrl = result[0].compressPath
                            for (item in result) {
                                var path = item.compressPath.ifEmpty {
                                    item.androidQToPath
                                }
                                mAdapter.addData(0, path)
                            }
                        }
                    }
                }

            }
        }
    }

    override fun liveObserver() {
        mViewModel.uploadImage.observe(this) {
            //跳转评估记录
            bundle.clear()
            RouteManager.goAct(ARouterUrl.ESTIMATE_RECORD, bundle)
            finish()
        }

        mViewModel.imageLive.observe(this) {
            it.data?.let { list ->
                orderNo?.let {
                    mViewModel.uploadImages(it, list)
                }
            }
        }

        mViewModel.isCanReservationOb.observe(this) {
            it.data?.let {
                if (it.equals("1")) {
                    //可预约
                    binding.getBlood.visibility = View.VISIBLE
                    binding.title.mTvRight.visibility = View.VISIBLE
                } else{
                    //不可预约
                    binding.getBlood.visibility = View.GONE
                    binding.title.mTvRight.visibility = View.GONE
                }
            }
        }
    }
}