package com.zj365.dc.activity

import android.content.Intent
import android.graphics.Rect
import android.view.LayoutInflater

import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SizeUtils
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.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.model.ReportBean
import com.xty.common.model.ReportNewBean
import com.xty.common.picture.PictureUtils
import com.xty.network.model.FoodRecoginzeInfo
import com.zj365.dc.BuildConfig
import com.zj365.dc.adapter.RecognizeResultAdapter
import com.zj365.dc.databinding.ActRecognizeResultBinding
import com.zj365.dc.vm.DietManagementVm
import com.zj365.health.R

@Route(path = ARouterUrl.RECOGNIZE_RESULT)
class RecognizeResultAct : BaseVmAct<DietManagementVm>() {

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

    val adapter by lazy { RecognizeResultAdapter() }

    var bottomSheetDialog: BottomSheetDialog? = null

    var type:String? = null
    var fileUrl :String? = null


    var foodRecoginzeInfo: FoodRecoginzeInfo? = null

    override fun liveObserver() {
        mViewModel.foodRecoginzeInfoLiveData.observe(this){
            adapter.data.clear()
            foodRecoginzeInfo = null
            if (!it.data.isNullOrEmpty()){
                it.data[0].isCheck = true
                adapter.setNewInstance(it.data)
                refreshCalorieUI(it.data[0])
                foodRecoginzeInfo = it.data[0]
            }

        }
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "识别结果"

        type = intent.getStringExtra("type")
        fileUrl = intent.getStringExtra("fileUrl")

        fileUrl?.let {
            mViewModel.submit(type!!,it)
        }

        binding.title.mIvBack.setOnClickListener {
            finish()
        }
        initAdapter()
        binding.tvPhotographAgain.setOnClickListener {
            bottomSheetDialog?.show()
        }
        binding.tvDetail.setOnClickListener {
            foodRecoginzeInfo?.let {
                bundle.clear()
                bundle.putString("web_site_url","${BuildConfig.h5url}pages/index/index?pageId=mealsDetail&id=${it.recipe_id}")
                RouteManager.goAct(ARouterUrl.H5_ACT,bundle)
            }


        }

        initBottomDialog()

    }

    fun refreshCalorieUI(bean : FoodRecoginzeInfo){
        binding.tvAnalyzeTitle.text = bean.name
        binding.tvCalorie.text ="${bean.heat}kcal/100g"
        var vit = bean.vit_a+ bean.vit_b1+ bean.vit_b2+bean.vit_c+bean.vit_e
       var calorieBean =  ReportNewBean().apply {
            title = "热卡分析"
            count = bean.protein+bean.cho+bean.fat
            listNum.add(bean.cho)
            listNum.add(bean.protein)
            listNum.add(bean.fat)
            listName.add("碳水化合")
            listName.add("蛋白质")
            listName.add("脂肪")
           unit = "g"
           isShow = true
        }

        binding.mCircle.setData(calorieBean)
        binding.mCircle.isVisible = calorieBean.isShow

    }

    fun initAdapter(){
        binding.recycler.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
        binding.recycler.adapter = adapter
        binding.recycler.addItemDecoration(object : ItemDecoration(){
            override fun getItemOffsets(outRect: Rect, itemPosition: Int, parent: RecyclerView) {
                super.getItemOffsets(outRect, itemPosition, parent)
                outRect.right = SizeUtils.dp2px(8.0f)
            }
        })
        adapter.setOnItemClickListener{ adapter, view, position ->
            foodRecoginzeInfo = adapter.getItem(position) as FoodRecoginzeInfo

            (adapter.data as MutableList<FoodRecoginzeInfo>).forEach {
                it.isCheck = false
            }
            foodRecoginzeInfo?.let {
                it.isCheck = true
                refreshCalorieUI(it)
            }

            adapter.notifyDataSetChanged()
        }
    }

    override fun initData() {
        super.initData()
    }

    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 {
                               mViewModel.submit(type!!,it)
                            }

                        }
                    }
                }

            }
        }
    }
}