package com.zj365.health.fragment

import android.app.Activity.RESULT_OK
import android.content.Intent
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.xty.base.fragment.BaseVmFrag
import com.xty.common.TimeSelect
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.picture.PictureUtils
import com.xty.common.recycleUtils.RecycleItem
import com.xty.common.setImage
import com.xty.common.util.CommonToastUtils
import com.zj365.health.R
import com.zj365.health.adapter.ImageAdapter
import com.zj365.health.adapter.TimeStatusAdapter
import com.zj365.health.databinding.FragDataUpload1Binding
import com.zj365.health.vm.DataUploadVm

/**
 * 数据上传
 */
class DataUploadFrag : BaseVmFrag<DataUploadVm>() {

    lateinit var clickCamera: View //点击拍照
    var isList = false; //是否列表
    val adapter by lazy { TimeStatusAdapter() }

    //val adapterEat by lazy { TimeStatusAdapter() }
    val adapterMedic by lazy { TimeStatusAdapter() }
    val timeSelect by lazy {
        TimeSelect(requireContext()) {

        }
    }
    val binding by lazy { FragDataUpload1Binding.inflate(layoutInflater) }
    val timeArray by lazy { resources.getStringArray(R.array.time_array) }
    val eatArray by lazy { resources.getStringArray(R.array.eat_array) }
    val medicArray by lazy { resources.getStringArray(R.array.medic_array) }

    override fun setLayout() = binding.root

    override fun initView() {
        super.initView()
        upload1Event()
//        initDbpUpload2()
//        initUpload3Recycle()
        initUpload4Recycle()
        binding.upload1.mHistory1.setOnClickListener {
            RouteManager.goAct(ARouterUrl.ELECT_LIST)
        }
//        binding.upload2.mHistoryXy.setOnClickListener {
//            bundle.clear()
//            bundle.putInt("type",1)
//            RouteManager.goAct(ARouterUrl.BLOOD_INFO,bundle)
//        }
//        binding.upload3.mHistory.setOnClickListener {
//            bundle.clear()
//            bundle.putInt("type",2)
//            RouteManager.goAct(ARouterUrl.BLOOD_INFO,bundle)
//        }
        binding.upload4.mHistory.setOnClickListener {
            RouteManager.goAct(ARouterUrl.MEDIC_LIST)
        }
    }

    override fun setViewModel() = DataUploadVm()

    override fun observer() {
        mViewModel.uploadPhoto.observe(this) {
            binding.upload1.mUpload.isEnabled = true
            CommonToastUtils.showSucceedToast("电子化验单上传成功")
        }
//        mViewModel.uploadDbp.observe(this) {
//            binding.upload2.mUpload.isEnabled=true
//            ToastUtils.show("血压上传成功")
//        }
//        mViewModel.uploadClu.observe(this) {
//            binding.upload3.mUpload.isEnabled=true
//            ToastUtils.show("血糖上传成功")
//        }
        mViewModel.uploadDrug.observe(this) {
            binding.upload4.mUpload.isEnabled = true
            CommonToastUtils.showSucceedToast("用药上传成功")
        }
    }

    /**
     * 电子化验单上传
     */
    private fun upload1Event() {

        initAdapter(binding.upload1.mRecycle1)
        initAdapter(binding.upload1.mRecycle2)
        initAdapter(binding.upload1.mRecycle3)
        initAdapter(binding.upload1.mRecycle4)
        initAdapter(binding.upload1.mRecycle5)
        initAdapter(binding.upload1.mRecycle6)
        binding.upload1.mTvPhoto1.setOnClickListener { openCamera(binding.upload1.mRecycle1) }
        binding.upload1.mTvPhoto2.setOnClickListener { openCamera(binding.upload1.mRecycle2) }
        binding.upload1.mTvPhoto3.setOnClickListener { openCamera(binding.upload1.mRecycle3) }
        binding.upload1.mTvPhoto4.setOnClickListener { openCamera(binding.upload1.mRecycle4) }
        binding.upload1.mTvPhoto5.setOnClickListener { openCamera(binding.upload1.mRecycle5) }
        binding.upload1.mTvPhoto6.setOnClickListener { openCamera(binding.upload1.mRecycle6) }
        binding.upload1.mUpload.setOnClickListener {
            it.isEnabled = false
            mViewModel.upload(
                (binding.upload1.mRecycle1.tag as ImageAdapter).data as MutableList<LocalMedia>,
                (binding.upload1.mRecycle2.tag as ImageAdapter).data as MutableList<LocalMedia>,
                (binding.upload1.mRecycle3.tag as ImageAdapter).data as MutableList<LocalMedia>,
                (binding.upload1.mRecycle4.tag as ImageAdapter).data as MutableList<LocalMedia>,
                (binding.upload1.mRecycle5.tag as ImageAdapter).data as MutableList<LocalMedia>,
                (binding.upload1.mRecycle6.tag as ImageAdapter).data as MutableList<LocalMedia>,
            )
        }
    }

    /**
     * 拍照
     */
    private fun initAdapter(recycle: RecyclerView) {
        val mAdapter = ImageAdapter()
        val item = RecycleItem(requireContext(), 5, 1)
        item.setSpace(10, 10)
        recycle.adapter = mAdapter
        recycle.addItemDecoration(item)
        recycle.layoutManager = GridLayoutManager(requireContext(), 5)
        recycle.tag = mAdapter
        mAdapter.addChildClickViewIds(R.id.mClose)
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.mClose -> {
                    adapter.removeAt(position)
                    adapter.notifyItemRemoved(position)
                }
            }
        }
    }

//    /**
//     * 血压上传
//     */
//    private fun initDbpUpload2() {
//        binding .upload2.mTvTime.setOnClickListener {
//            RxKeyboardTool.hideSoftInput(requireActivity())
//            timeSelect.selectTime(it,"yyyy-MM-dd HH:mm:ss")
//        }
//        val recycleItem = RecycleItem(requireContext(), 4, 1)
//        recycleItem.setSpace(40, 40)
//        binding.upload2.mRecycle.addItemDecoration(recycleItem)
//        binding.upload2.mRecycle.layoutManager = GridLayoutManager(requireContext(), 3)
//        binding.upload2.mRecycle.adapter = adapterMedic
//        adapterMedic.setOnItemClickListener { _, _, position ->
//            adapterMedic.positionSelect = position
//            adapterMedic.notifyDataSetChanged()
//        }
//        adapterMedic.setNewInstance(medicArray.toMutableList())
//
//        binding.upload2.mUpload.setOnClickListener {
//            it.isEnabled=false
//            val dbp = binding.upload2.mEditDbp.text.toString().trim()
//            val sbp = binding.upload2.mEditSbp.text.toString().trim()
//            var time= binding.upload2.mTvTime.text.toString()
//            var medic=timeArray[adapter.positionSelect]
//            mViewModel.uploadDbp(dbp, sbp,time,medic)
//        }
//    }
//
//    /**
//     * 血糖测量时间
//     */
//    private fun initUpload3Recycle() {
//        val recycleItem = RecycleItem(requireContext(), 4, 1)
//        recycleItem.setSpace(40, 40)
//        binding.upload3.mRecycle.addItemDecoration(recycleItem)
//        binding.upload3.mRecycle.layoutManager = GridLayoutManager(requireContext(), 4)
//        binding.upload3.mRecycle.adapter = adapter
//        adapter.setOnItemClickListener { _, _, position ->
//            adapter.positionSelect = position
//            adapter.notifyDataSetChanged()
//        }
//        adapter.setNewInstance(timeArray.toMutableList())
//        binding.upload3.mTvTest3.setOnClickListener {
//            RxKeyboardTool.hideSoftInput(requireActivity())
//            timeSelect.selectTime(it,"yyyy-MM-dd HH:mm:ss")
//        }
//        binding.upload3.mUpload.setOnClickListener {
//            it.isEnabled=false
//            var num= binding.upload3.mTvTest1.text.toString().trim()
//            var time=binding.upload3.mTvTest3.text.toString().trim()
//            var type=timeArray[adapter.positionSelect]
//            var remark=  binding.upload3.mTvTest4.text.toString().trim()
//            mViewModel.uploadCluInfo(num,time,type,remark)
//        }
//    }

    /**
     * 用药上传
     */
    private fun initUpload4Recycle() {
        val recycleItem = RecycleItem(requireContext(), 4, 1)
        recycleItem.setSpace(40, 40)
        initAdapter(binding.upload4.mRecycle)
        binding.upload4.mTvTest1.setOnClickListener { openCamera(binding.upload4.mRecycle) }

        binding.upload4.mUpload.setOnClickListener {
            it.isEnabled = false
            val list =
                (binding.upload4.mRecycle.tag as ImageAdapter).data as MutableList<LocalMedia>
            mViewModel.uploadDrugInfo(list)

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode === RESULT_OK) {
            when (requestCode) {
                PictureConfig.CHOOSE_REQUEST -> {
                    val result = PictureSelector.obtainMultipleResult(data)
                    if (result.size > 0) {
                        if (isList) {
                            isList = false
                            val adapter = clickCamera.tag as ImageAdapter
                            adapter.setNewInstance(result as MutableList<Any>)
                        } else {
                            clickCamera.tag = result[0].compressPath
                            (clickCamera as ImageView).setImage(
                                requireContext(),
                                result[0].compressPath
                            )
                        }
                    }
                }
            }
        }
    }


    fun openCamera(view: View) {
        clickCamera = view
        if (view is RecyclerView) {
            isList = true
            val adapter = view.tag as ImageAdapter
            PictureUtils.openCameraShow(
                requireActivity(),
                PictureMimeType.ofImage(),
                5,
                adapter.data as MutableList<LocalMedia>
            )
        } else {
            PictureUtils.openCamera(requireActivity())
        }
    }


}