package com.develop.food.base.widgets

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import com.develop.base.R
import com.develop.base.databinding.HomeFunctionTopBarViewBinding
import com.develop.food.base.data.FunType
import com.develop.food.base.utils.ResDimension

class HomeFunctionTopBar : RelativeLayout, View.OnClickListener {
    private var binding: HomeFunctionTopBarViewBinding

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    private var funType = FunType.LocalRecipes
    var onHomeFunctionClickListener: OnHomeFunctionClickListener? = null

    private var tempFunType = FunType.LocalRecipes

    init {
        val root = View.inflate(context, R.layout.home_function_top_bar_view, this)
        binding = HomeFunctionTopBarViewBinding.bind(root)
        binding.ivNotify.setOnClickListener(this)
        binding.userLayout.setOnClickListener(this)
        binding.modeLayout.setOnClickListener(this)
        binding.ivTab.setOnClickListener(this)
        binding.ivWifi.setOnClickListener(this)
        binding.ivWifi.setImageResource(R.drawable.icon_wifi_signal_level)
        binding.ivWifi.setImageLevel(0)
    }

    fun setCurrentFunType(type: FunType) {
        funType = type
        tempFunType = funType
        when (type) {
            FunType.Modes -> {
                updateIconSize(false)
                binding.ivFunction.setBackgroundResource(R.drawable.ic_recipes)
                binding.functionName.text = resources.getString(R.string.recipes)
            }
            FunType.LocalRecipes -> {
                updateIconSize(false)
                binding.ivFunction.setBackgroundResource(R.drawable.ic_mode)
                binding.functionName.text = resources.getString(R.string.mode)
            }
            else -> {}
        }
    }

    /**
     * 切换网络菜谱时调用
     */
    fun updateNetFunType() {
        funType = FunType.NetRecipes
        updateIconSize(true)
        binding.ivFunction.setBackgroundResource(R.drawable.ic_white_back)
        binding.functionName.text = resources.getString(R.string.local_recipes)
    }

    interface OnHomeFunctionClickListener {
        fun onModeClick(funType: FunType)
        fun onWifiClick()
        fun onUserClick()
        fun onTabClick()
        fun onNotifyClick()
    }

    override fun onClick(p0: View) {
        when (p0.id) {
            R.id.mode_layout -> {
                when (funType) {
                    FunType.Modes -> {
                        funType = FunType.LocalRecipes
                        tempFunType = funType
                        updateIconSize(false)
                        binding.ivFunction.setBackgroundResource(R.drawable.ic_mode)
                        binding.functionName.text = resources.getString(R.string.mode)
                    }
                    FunType.LocalRecipes -> {
                        updateIconSize(false)
                        funType = FunType.Modes
                        tempFunType = funType
                        binding.ivFunction.setBackgroundResource(R.drawable.ic_recipes)
                        binding.functionName.text = resources.getString(R.string.recipes)
                    }
                    FunType.NetRecipes -> {
                        updateIconSize(false)
                        funType = tempFunType
                        if (funType == FunType.LocalRecipes) {
                            binding.ivFunction.setBackgroundResource(R.drawable.ic_mode)
                            binding.functionName.text = resources.getString(R.string.mode)
                        } else {
                            binding.ivFunction.setBackgroundResource(R.drawable.ic_recipes)
                            binding.functionName.text = resources.getString(R.string.recipes)
                        }
                    }
                }
                onHomeFunctionClickListener?.onModeClick(funType)
            }
            R.id.user_layout -> {
                onHomeFunctionClickListener?.onUserClick()
            }
            R.id.iv_wifi -> {
                onHomeFunctionClickListener?.onWifiClick()
            }
            R.id.iv_notify -> {
                onHomeFunctionClickListener?.onNotifyClick()
            }
            R.id.iv_tab -> {
                onHomeFunctionClickListener?.onTabClick()
            }
        }
    }

    fun getNetView(): ImageView {
        return binding.ivWifi
    }

    private fun updateIconSize(isLocal: Boolean) {
        val param = binding.ivFunction.layoutParams as LayoutParams
        if (isLocal) {
            param.width = ResDimension(R.dimen.convert_35px).toInt()
            param.height = ResDimension(R.dimen.convert_60px).toInt()
        } else {
            param.width = ResDimension(R.dimen.convert_120px).toInt()
            param.height = ResDimension(R.dimen.convert_120px).toInt()
        }
    }
}