package com.zj365.mime.act

import android.content.Intent
import android.view.View
import androidx.lifecycle.MutableLiveData
import com.alibaba.android.arouter.facade.annotation.Route
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.tamsiree.rxkit.RxKeyboardTool
import com.xty.base.act.BaseVmAct
import com.xty.common.Const
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.event.MyInfoEvent
import com.xty.common.picture.PictureUtils
import com.xty.common.setImage
import com.zj365.mime.BuildConfig
import com.zj365.mime.R
import com.zj365.mime.databinding.ActMyInfoBinding
import com.zj365.mime.vm.MyInfoVm
import com.xty.network.model.SettingBean
import org.greenrobot.eventbus.EventBus
import java.io.File

/**
 * 个人信息修改
 */
@Route(path = ARouterUrl.MY_INFO)
class MyInfoAct: BaseVmAct<MyInfoVm>() {

    val defaultIcon by lazy { arrayOf(R.mipmap.ic_default_man,R.mipmap.ic_default_women)}
    val binding by lazy{ ActMyInfoBinding.inflate(layoutInflater)}
    var user: SettingBean.User?=null
    var changeInfoType="" //修改的信息类型
    var changeInfo="" //修改的信息
    var diseaseList="" //病史手术 信息
    override fun setLayout()=binding.root

    companion object{
        val nameLive by lazy { MutableLiveData<String>() }
    }

    override fun initData() {
        super.initData()
        isDarkBar=true

        intent.extras?.let {
            var tag=intent.extras!!.getSerializable("user")
            if(tag !=null){
                user =tag as SettingBean.User
            }
        }

    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text=getString(R.string.info_change)
        binding.title.mIvBack.setOnClickListener {
            finish()
        }
        binding.mImage.setOnClickListener {
            hideInputSoft()
            PictureUtils.openCamera(this)
        }
        binding.mTvName.setOnClickListener {
            hideInputSoft()
            RouteManager.goAct(ARouterUrl.NICK_INFO)
        }
        user?.let {
            if(!it.avatarUrl.isNullOrEmpty()){
                binding.mImage.setImage(this,it.avatarUrl)
            }else{
                binding.mImage.setImageResource(R.mipmap.ic_default_man)
            }
            binding.mTvName.text=it.name
            binding.mHeight.text="${it.height}cm"
            binding.mWeight.text="${it.weight}kg"
            binding.mHistoryMedic.text=if(it.medicalHistory.isNullOrEmpty()) "" else it.medicalHistory
        }
        binding.mHeight.setOnClickListener {
            changeInfoType="height"
            shouInputSoft("请输入身高（cm）")

        }
        binding.mWeight.setOnClickListener {
            changeInfoType="weight"
            shouInputSoft("请输入体重（kg）")
        }
        binding.mConfirm.setOnClickListener {
            val trim = binding.mEditInfo.text.toString().trim()
            if(trim.isNullOrEmpty()){
                return@setOnClickListener
            }
            changeInfo=trim
            mViewModel.changeInfo(changeInfoType,changeInfo)
            hideInputSoft()
        }
        binding.mHistoryMedic.setOnClickListener {
            hideInputSoft()
            RouteManager.goActRequest( ARouterUrl.MEDICAL_HISTORY,this,10000)
        }
    }

    override fun keyBoardHind(isPopup: Boolean) {
        super.keyBoardHind(isPopup)
        if(!isPopup){
            hideInputSoft()
        }
    }

    private fun shouInputSoft(inputHint:String) {
        (binding.mEditInfo.parent as View).visibility=View.VISIBLE
        binding.mEditInfo.isFocusable=true
        binding.mEditInfo.hint = inputHint
        binding.mEditInfo.isSelected=true
        RxKeyboardTool.showSoftInput(this,  binding.mEditInfo)

    }
    private fun hideInputSoft() {
        (binding.mEditInfo.parent as View).visibility=View.GONE
        binding.mEditInfo.setText("")
        binding.mEditInfo.clearFocus()
        binding.mEditInfo.isSelected=false
        RxKeyboardTool.hideSoftInput(this)
    }

    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){
                        var path=if(result[0].compressPath.isEmpty()){
                            result[0].androidQToPath
                        }else{
                            result[0].compressPath
                        }
                        mViewModel.changeImage(path)
                        binding.mImage.tag=path
                    }
                }
                10000->{
                    var ids = data!!.extras!!.getString("ids")
                    val names = data!!.extras!!.getString("names")
                    diseaseList= data!!.extras!!.getString("diseaseList").toString()
                    binding.mHistoryMedic.tag=names
                    if(ids!!.isNotEmpty()){
                        mViewModel.changeMedic(ids,names!!)
                    }
                }
            }
        }
    }

    override fun liveObserver() {
        nameLive.observe(this){
            changeInfoType="name"
            changeInfo=it
            mViewModel.changeInfo(changeInfoType,it)

        }
        mViewModel.changeMedicSuccess.observe(this){
            var names=binding.mHistoryMedic.tag as String
            binding.mHistoryMedic.text=names
            EventBus.getDefault().post(MyInfoEvent())
        }
        mViewModel.changeSuccess.observe(this){
            when(changeInfoType){
                "name"->    binding.mTvName.text=changeInfo
                "height"->  binding.mHeight.text="${changeInfo}cm"
                "weight"->binding.mWeight.text="${changeInfo}kg"
            }
            EventBus.getDefault().post(MyInfoEvent())

        }

        mViewModel.imageLive.observe(this){
            EventBus.getDefault().post(MyInfoEvent())
            binding.mImage.setImage(this, binding.mImage.tag.toString())
        }
    }
}