package com.develop.common.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import com.develop.base.ext.load
import com.develop.base.R.*
import com.develop.base.ext.resId2Dimension
import com.develop.base.ext.updateText
import com.develop.common.R
import com.develop.common.databinding.HomeFunctionTopBarViewBinding
import com.develop.common.data_repo.db.RecipesType


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
    ) {
    }

    var onHomeFunctionClickListener: OnHomeFunctionClickListener? = null

    private var recipesType = RecipesType.LOCAL
    private var homeOrBackType = HomeOrBack.Home

    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_act_signal_level)
        binding.ivWifi.setImageLevel(0)
        binding.ivFunction.load(R.drawable.ic_home)

    }

    fun setHomeOrBack(type: HomeOrBack) {
        homeOrBackType = type
        if (type == HomeOrBack.Home) {
            binding.ivFunction.load(R.drawable.ic_home)
            binding.functionName.text = resources.getString(R.string.home)
        } else {
            binding.ivFunction.load(R.drawable.ic_white_back)
            binding.functionName.text = resources.getString(R.string.back)
        }
    }


    interface OnHomeFunctionClickListener {
        fun onWifiClick()
        fun onUserClick()
        fun onTabClick()
        fun onNotifyClick()
        fun onHomeOrBackClick(homeOrBack: HomeOrBack)
    }

    override fun onClick(p0: View) {
        when (p0.id) {
            R.id.mode_layout -> {
                onHomeFunctionClickListener?.onHomeOrBackClick(homeOrBackType)
            }
            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
    }

    fun setNetRssi(level: Int) {
        binding.ivWifi.setImageLevel(level)
    }

    fun setTitle(title: String) {
        binding.tvTitle.updateText(title)
    }

    fun setUserName(userName: String) {
        binding.tvState.text = userName
    }

    private fun updateIconSize(isLocal: Boolean) {
        val param = binding.ivFunction.layoutParams as LayoutParams
        if (isLocal) {
            param.width = dp35
            param.height = dp60
        } else {
            param.width = dp120
            param.height = dp120
        }
    }

    companion object {
        var dp35 = (dimen.convert_35px).resId2Dimension().toInt()
        var dp60 = (dimen.convert_60px).resId2Dimension().toInt()
        var dp120 = (dimen.convert_120px).resId2Dimension().toInt()
    }
}

enum class HomeOrBack {
    Home,
    Back
}