package com.zj365.health.fragment.tongue

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.xty.base.fragment.BaseVmFrag
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.act.tonguediagnosis.TongueDetailActivity
import com.zj365.health.databinding.FragmentTongueUserInfoBinding
import com.zj365.health.vm.TongueDiagnosisVm
import com.xty.network.model.CancerGoodsBean

class TongueUserInfoFragment : BaseVmFrag<TongueDiagnosisVm>() {

    val binding by lazy { FragmentTongueUserInfoBinding.inflate(layoutInflater) }
    override fun setViewModel() = TongueDiagnosisVm()
    override fun setLayout() = binding.root

    lateinit var bean: CancerGoodsBean

    var isMan = true

    var isTop = true   //选择照片标签  true为舌面  false为舌底
    var topSelectImg = ""
    var bottomSelectImg = ""

    companion object {
        var orderNoKey: String = "orderNoKey"
        fun newInstance(bean: CancerGoodsBean): Fragment{
            val args = Bundle()
            args.putSerializable(orderNoKey,bean)
            val fragment = TongueUserInfoFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun initView() {
        super.initView()
        initMyView()

    }

    fun initMyView(){
        bean = (activity as TongueDetailActivity).bean
        binding.etName.setText(bean.realName)
        binding.etAge.setText(bean.age)
        if (bean.sex.equals("1")){
            binding.ivSex.setImageResource(R.mipmap.xingbie_nan)
            isMan = true
        } else {
            binding.ivSex.setImageResource(R.mipmap.xingbie_nv)
            isMan = false
        }
        binding.ivTongueTop.setImage(this.requireContext(),bean.imageUrl)
        binding.ivTongueBottom.setImage(this.requireContext(),bean.backImageUrl)
        binding.tvErrorInfo.text = bean.preMsg

        when (bean.status) {
            6,2 -> {
                binding.etName.isEnabled = true
                binding.etAge.isEnabled = true
                binding.ivSex.isEnabled = true
                binding.ivTongueTop.setOnClickListener{
                    isTop = true
                    PictureUtils.openCamera(this.requireActivity(), PictureMimeType.ofImage())
                }
                binding.ivTongueBottom.setOnClickListener{
                    isTop = false
                    PictureUtils.openCamera(this.requireActivity(), 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
                }
                binding.ivDeleteTop.visibility = View.VISIBLE
                binding.ivDeleteBottom.visibility = View.VISIBLE
                binding.tvSubmit.visibility = View.VISIBLE
                binding.clErrorInfo.visibility = View.VISIBLE
                binding.llHintLayout.visibility = View.VISIBLE
                binding.ivSex.setOnClickListener {
                    binding.ivSex.setImageResource(if (isMan) R.mipmap.xingbie_nv else R.mipmap.xingbie_nan)
                    isMan = !isMan
                }
                binding.tvSubmit.setOnClickListener {
                    submitInfo()
                }
            }
            else -> {
                binding.etName.isEnabled = false
                binding.etAge.isEnabled = false
                binding.ivSex.isEnabled = false
                binding.ivTongueTop.setOnClickListener{}
                binding.ivTongueBottom.setOnClickListener{}
                binding.ivSex.setOnClickListener {}
                binding.tvSubmit.setOnClickListener {}
                binding.ivDeleteBottom.visibility = View.GONE
                binding.ivDeleteTop.visibility = View.GONE
                binding.tvSubmit.visibility = View.GONE
                binding.clErrorInfo.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 === AppCompatActivity.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.requireContext(),topSelectImg)
                                    binding.ivDeleteTop.visibility = View.VISIBLE
                                } else {
                                    bottomSelectImg = path
                                    binding.ivTongueBottom.setImage(this.requireContext(),bottomSelectImg)
                                    binding.ivDeleteBottom.visibility = View.VISIBLE
                                }
                            }
                        }
                    }
                }
            }
        }
    }

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

        mViewModel.upDateInfo.observe(this){
            (activity as TongueDetailActivity).getOrderDetailInfo()
        }
    }

}