package com.zj365.dc.activity

import android.content.Intent
import android.view.LayoutInflater
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.android.material.bottomsheet.BottomSheetDialog
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.base.vm.BaseVm
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.picture.PictureUtils
import com.zj365.dc.databinding.ActCalorieAnalyzeBinding
import com.zj365.dc.vm.DietManagementVm
import com.zj365.health.R

@Route(path = ARouterUrl.CALORIE_ANALYZE)
class CalorieAnalyzeAct : BaseVmAct<BaseVm>() {

    val binding by lazy { ActCalorieAnalyzeBinding.inflate(layoutInflater) }

    var type:String = "1"//类型 1-菜品 2-果蔬 3-零食

    var fileUrl:String? = null //当前选择的文件的路径


    var bottomSheetDialog: BottomSheetDialog? = null
    override fun liveObserver() {

    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "热卡分析"
        binding.title.mIvBack.setOnClickListener {
            finish()
        }

        binding.tvDishes.setOnClickListener {
            bottomSheetDialog?.show()
            type = "1"
        }

        binding.tvFruitVegetable.setOnClickListener {
            bottomSheetDialog?.show()
            type = "2"

        }
        binding.tvSnack.setOnClickListener {
            bottomSheetDialog?.show()
            type = "3"

        }

        initBottomDialog()
    }

    override fun setLayout() = binding.root

    fun initBottomDialog() {
        if (bottomSheetDialog == null) {
            bottomSheetDialog = BottomSheetDialog(this, R.style.Theme_ChartDialogManager)
            bottomSheetDialog?.let { dialog ->
                val view = LayoutInflater.from(this).inflate(R.layout.dialog_choose_pic, null)
                dialog.setContentView(view)

                val tvTakePhoto = view.findViewById<TextView>(R.id.tv_take_photo)
                val tvChoosePhotoAlbum = view.findViewById<TextView>(R.id.tv_choose_photo_album)
                val tvCancel = view.findViewById<TextView>(R.id.tv_cancel)
                tvCancel.setOnClickListener {
                    dialog.dismiss()
                }

                tvTakePhoto.setOnClickListener {
                    PictureUtils.openTheCamera(this, PictureMimeType.ofImage())
                    dialog.dismiss()
                }

                tvChoosePhotoAlbum.setOnClickListener {
                    PictureUtils.openCamera(this, PictureMimeType.ofImage(),9)
                    dialog.dismiss()

                }

            }
        }

    }

    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()) {
                            fileUrl = result[0].compressPath

                            fileUrl?.let {
                                bundle.clear()
                                bundle.putString("type",type)
                                bundle.putString("fileUrl",fileUrl)
                                RouteManager.goAct(ARouterUrl.RECOGNIZE_RESULT,bundle)
                            }

                        }
                    }
                }

            }
        }
    }
}