package com.zj365.health.act.tonguediagnosis

import android.content.Intent
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.luck.picture.lib.PictureSelector
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.xty.common.setImage
import com.xty.common.util.CommonToastUtils
import com.zj365.health.R
import com.zj365.health.databinding.ActivityEditTongueInfoBinding
import com.zj365.health.vm.TongueDiagnosisVm

@Route(path = ARouterUrl.TONGUE_EDIT_INFO_ACTIVITY)
class EditTongueInfoActivity : BaseVmAct<TongueDiagnosisVm>() {
    
    val binding by lazy { ActivityEditTongueInfoBinding.inflate(layoutInflater) }

    override fun setLayout() = binding.root
    var isMan = true
    var orderNo: String? = ""
    var isTop = true   //选择照片标签  true为舌面  false为舌底
    var topSelectImg = ""
    var bottomSelectImg = ""

    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.ivSex.setOnClickListener {
            binding.ivSex.setImageResource(if (isMan) R.mipmap.xingbie_nv else R.mipmap.xingbie_nan)
            isMan = !isMan
        }
        binding.tvSubmit.setOnClickListener {
            submitInfo()
        }
        binding.ivTongueTop.setOnClickListener {
            isTop = true
            PictureUtils.openCamera(this, PictureMimeType.ofImage())
        }
        binding.ivTongueBottom.setOnClickListener {
            isTop = false
            PictureUtils.openCamera(this, PictureMimeType.ofImage())
        }
        binding.ivDeleteTop.setOnClickListener {
            topSelectImg = ""
            binding.ivTongueTop.setImageResource(R.mipmap.changctup)
            binding.ivDeleteTop.visibility = View.GONE
        }
        binding.ivDeleteBottom.setOnClickListener {
            bottomSelectImg = ""
            binding.ivTongueBottom.setImageResource(R.mipmap.changctup)
            binding.ivDeleteBottom.visibility = View.GONE
        }
    }

    private fun submitInfo() {
        if (binding.etName.text.isNullOrEmpty()) {
            CommonToastUtils.showToast("请输入姓名")
            return
        }

        if (binding.etAge.text.isNullOrEmpty()) {
            CommonToastUtils.showToast("请输入年龄")
            return
        }

        if (topSelectImg.isNullOrEmpty()) {
            CommonToastUtils.showToast("请选择舌面照片")
            return
        }

        if (bottomSelectImg.isNullOrEmpty()) {
            CommonToastUtils.showToast("请选择舌下照片")
            return
        }
        var imgPaths = arrayListOf<String>()
        imgPaths.add(topSelectImg)
        imgPaths.add(bottomSelectImg)
        mViewModel.uploadImagesFiles(imgPaths)


    }

    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()) {
                            for (item in result) {
                                var path = item.compressPath.ifEmpty {
                                    item.androidQToPath
                                }
                                if (isTop) {
                                    topSelectImg = path
                                    binding.ivTongueTop.setImage(this, topSelectImg)
                                    binding.ivDeleteTop.visibility = View.VISIBLE
                                } else {
                                    bottomSelectImg = path
                                    binding.ivTongueBottom.setImage(this, bottomSelectImg)
                                    binding.ivDeleteBottom.visibility = View.VISIBLE
                                }
                            }
                        }
                    }
                }

            }
        }
    }


    override fun liveObserver() {
        mViewModel.imagePath.observe(this) {
            if (it.data.size == 2) {
                topSelectImg = it.data[0]
                bottomSelectImg = it.data[1]
                mViewModel.updateTongueOrder(
                    orderNo!!,
                    if (isMan) "1" else "2",
                    binding.etAge.text.toString(),
                    binding.etName.text.toString(),
                    topSelectImg,
                    bottomSelectImg
                )
            } else {
                CommonToastUtils.showToast("请重试！")
            }
        }

        mViewModel.upDateInfo.observe(this) {
            finish()
        }
    }
}