package com.develop.step.ui.recipes_detail

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.KeyboardUtils
import com.develop.base.util.GlobalToast
import com.develop.common.data_repo.FoodDataProvider
import com.develop.common.data_repo.db.entity.UserTag
import com.develop.common.router.Screens
import com.develop.common.tag.CURRENT_USER_ID
import com.develop.common.ui.CommonBindingActivity
import com.develop.common.R

import com.develop.step.databinding.ActivityCookEvaluateBinding

@Route(path = Screens.Cook.COOK_EVALUATE)
class CookEvaluateActivity : CommonBindingActivity<ActivityCookEvaluateBinding>() {
    override fun createViewBinding(inflater: LayoutInflater): ActivityCookEvaluateBinding {
        return ActivityCookEvaluateBinding.inflate(inflater)
    }

    private val starViews = mutableListOf<View>()

    @JvmField
    @Autowired(name = "recipeId")
    var recipeId = ""

    private var starCount = 0
    private var evaluateContent = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ARouter.getInstance().inject(this)

        binding.let {
            starViews.add(it.ivStar1)
            starViews.add(it.ivStar2)
            starViews.add(it.ivStar3)
            starViews.add(it.ivStar4)
            starViews.add(it.ivStar5)
        }

        binding.root.setOnClickListener {
            KeyboardUtils.hideSoftInput(this)
        }

        binding.ivStar1.setOnClickListener {
            selectStar(1)
            starCount = 1
            binding.tvGood.text = getString(R.string.bad)
        }
        binding.ivStar2.setOnClickListener {
            selectStar(2)
            starCount = 2
            binding.tvGood.text = getString(R.string.imperfect)
        }
        binding.ivStar3.setOnClickListener {
            selectStar(3)
            starCount = 3
            binding.tvGood.text = getString(R.string.ordinary)
        }
        binding.ivStar4.setOnClickListener {
            selectStar(4)
            starCount = 4
            binding.tvGood.text = getString(R.string.good)
        }
        binding.ivStar5.setOnClickListener {
            selectStar(5)
            starCount = 5
            binding.tvGood.text = getString(R.string.perfect)
        }
        binding.viewClose.setOnClickListener {
            finish()
        }

        binding.tvOk.setOnClickListener {
            KeyboardUtils.hideSoftInput(this)
            evaluateContent = binding.etContent.text.toString()
            FoodDataProvider.getUserDatabase().runInTransaction {
                val userTag = UserTag(CURRENT_USER_ID, recipeId, starCount, evaluateContent)
                FoodDataProvider.getUserDatabase().userInfoDao().updateUserTag(userTag)
            }
            GlobalToast.showToast("Evaluate successfully")

        }
    }

    private fun selectStar(count: Int) {
        for (index in starViews.indices) {
            starViews[index].isSelected = index + 1 <= count
        }
    }
}