package com.zj365.health.act

import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseVmAct
import com.xty.common.arouter.ARouterUrl
import com.xty.common.util.CommonToastUtils
import com.zj365.health.R
import com.zj365.health.adapter.QuestAdapter
import com.zj365.health.databinding.ActQuestListBinding
import com.zj365.health.vm.QuestListVm
import com.xty.network.model.QuestBean
import org.json.JSONArray
import org.json.JSONObject

/**
 * 问卷调查
 */
@Route(path = ARouterUrl.QUEST_LIST)
class QuestListAct: BaseVmAct<QuestListVm>() {
    var jsonArray= JSONArray()
    val binding by lazy{ActQuestListBinding.inflate(layoutInflater)}
    val mAdapter  by lazy{ QuestAdapter() }
    val topImage by lazy { arrayOf(R.mipmap.ic_step_1,R.mipmap.ic_step_2,R.mipmap.ic_step_3)}
    val subTitle by lazy{resources.getStringArray(R.array.step_title)}

    var secStep=false
    var thrStep=false
    override fun setLayout()=binding.root
    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text="风险评估"
        binding.title.mIvBack.setOnClickListener { finish() }
        initRecycle()
        binding.mNext.setOnClickListener {
            setStep()
        }
        mViewModel.getQuestList()
        changeTitle(1)


    }
    private fun initRecycle() {
        binding.mRecycle.layoutManager=LinearLayoutManager(this)
        binding.mRecycle.adapter=mAdapter
    }

    override fun liveObserver() {
        mViewModel.question.observe(this){
            it.data.questions.forEach { bean->
                bean.itemType=bean.typeId
            }
            getStepList(it.data,1)
        }
        mViewModel.subLive.observe(this){
            CommonToastUtils.showToast(R.string.quest_submit_success)
            setResult(RESULT_OK)
            finish()
        }

    }


    /**
     * 下一步 以及提交操作
     */
    private fun setStep() {
        if(mViewModel.question.value==null){
            return
        }

        if(!secStep){

            mAdapter.data.forEach {
                if(it.typeId==2&&it.selectPosition==-1){
                    CommonToastUtils.showToast("请完成所有调查选项")
                    return
                }
                if(it.typeId==4){
                    if ((it.weight.isEmpty()||it.height.isEmpty())){
                        CommonToastUtils.showToast("请填写身高体重信息")
                        return
                    }
                    if (it.weight.equals("0")||it.height.equals("0")||it.weight.equals("0.0")||it.height.equals("0.0")) {
                        CommonToastUtils.showToast("身高体重数据不能为0")
                        return
                    }
                }
                var json=JSONObject()
                json.put("templateId",mViewModel.question.value!!.data.templateId)
                json.put("questionId",it.questionId)
                when(it.typeId){
                    2-> json.put("optiont",it.selectOption)
                    4->json.put("optiont","${it.height},${it.weight}")
                 }
                jsonArray.put(json)
            }

            secStep=true
            binding.Step.setStep(secStep)
            getStepList(mViewModel.question.value!!.data,2)

            changeTitle(2)

        }else if(!thrStep){
            mAdapter.data.forEach {
                if(it.typeId==2&&it.selectPosition==-1){
                    CommonToastUtils.showToast("请完成所有调查选项")
                    return
                }
                if(it.typeId==4){
                    if ((it.weight.isEmpty()||it.height.isEmpty())){
                        CommonToastUtils.showToast("请填写身高体重信息")
                        return
                    }
                    if (it.weight.equals("0")||it.height.equals("0")||it.weight.equals("0.0")||it.height.equals("0.0")) {
                        CommonToastUtils.showToast("身高体重数据不能为0")
                        return
                    }
                }
                var json=JSONObject()
                json.put("templateId",mViewModel.question.value!!.data.templateId)
                json.put("questionId",it.questionId)
                when(it.typeId){
                    2-> json.put("optiont",it.selectOption)
                    4->json.put("optiont","${it.height},${it.weight}")
                }
                jsonArray.put(json)
            }

            thrStep=true
            binding.Step.setStep(secStep,thrStep)
            getStepList(mViewModel.question.value!!.data,3)
            changeTitle(3)
        }else{
            mAdapter.data.forEach {
                if(it.typeId==2&&it.selectPosition==-1){
                    CommonToastUtils.showToast("请完成所有调查选项")
                    return
                }
                if(it.typeId==4){
                    if ((it.weight.isEmpty()||it.height.isEmpty())){
                        CommonToastUtils.showToast("请填写身高体重信息")
                        return
                    }
                    if (it.weight.equals("0")||it.height.equals("0")||it.weight.equals("0.0")||it.height.equals("0.0")) {
                        CommonToastUtils.showToast("身高体重数据不能为0")
                        return
                    }
                }
                var json=JSONObject()
                json.put("templateId",mViewModel.question.value!!.data.templateId)
                json.put("questionId",it.questionId)
                when(it.typeId){
                    2-> json.put("optiont",it.selectOption)
                    4->json.put("optiont","${it.height},${it.weight}")
                }
                jsonArray.put(json)
            }


            mViewModel.submitQuest(jsonArray  )
        }

    }

    //数据重新组装
    private fun getStepList(it:QuestBean,step:Int) {
        var list= mutableListOf<QuestBean.Question>()
        it.questions.forEach {bean->
            if(bean.titleSort==step){
                list.add(bean)
            }
        }
        mAdapter.setNewInstance(list)
    }

    /**
     * 第几步 step
     */
    private fun changeTitle(step:Int) {
        binding.tvSteps.text = subTitle[step-1]
        binding.imgSteps.setImageResource(topImage[step-1])
        /*var draw=ContextCompat.getDrawable(this,topImage[step-1])
        draw!!.setBounds(0,0,draw!!.minimumWidth,draw!!.minimumHeight)
        binding.mTvStep.setCompoundDrawables(null,draw,null,null)*/
        if(step<3){
            binding.mNext.text=getString(R.string.next_step)
        }else{
            binding.mNext.text=getString(R.string.submit)
        }
    }
}