package com.abbidot.tracker.ui.activity.device

import android.content.Intent
import android.text.TextUtils
import android.view.View
import androidx.core.content.ContextCompat
import com.abbidot.baselibrary.constant.EventName
import com.abbidot.baselibrary.eventbus.XEventBus
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.databinding.ActivityAddNewTracker4Binding
import com.abbidot.tracker.util.ViewUtil
import com.daimajia.androidanimations.library.Techniques
import com.daimajia.androidanimations.library.YoYo

class AddNewTracker4Activity :
    BaseActivity<ActivityAddNewTracker4Binding>(ActivityAddNewTracker4Binding::inflate) {

    private var isFirstBind = false

    override fun getTopBar() = mViewBinding.ilAddNewTracker4TopBar.titleTopBar

    override fun initData() {
        super.initData()
        setTopBarTitle(R.string.txt_add_new_tracker)
        setLeftBackImage(R.drawable.icon_white_back_svg)

        intent.extras?.apply {
            isFirstBind = getBoolean(ConstantString.isFirstBind, false)
        }

        mViewBinding.apply {

            vetAddNewTracker4EditText.setEditClickListener {
                tvAddNewTracker4Error.visibility = View.GONE
                vetAddNewTracker4EditText.setBoxBackground(
                    ContextCompat.getDrawable(
                        mContext, R.drawable.shape8_gray_border_white_bg
                    )
                )
            }
            setOnClickListenerViews(btnAddNewTrackerAdd)
        }

        setListenKeyboardChange()
    }

    override fun liveDataObserve() {
        XEventBus.observe(this, EventName.RefreshDevice) {
            finish()
        }
    }

    private fun addTrackerError(errorTip: String) {
        mViewBinding.apply {
            vetAddNewTracker4EditText.let { v ->
                YoYo.with(Techniques.Shake).onStart {
                    v.setBoxBackground(
                        ContextCompat.getDrawable(
                            mContext, R.drawable.shape8_red_border_white_bg
                        )
                    )
                    if (tvAddNewTracker4Error.visibility == View.GONE) {
                        ViewUtil.instance.viewShowFadeInAnimation(btnAddNewTrackerAdd, 800)
                    }
                    ViewUtil.instance.viewShowFadeInAnimation(tvAddNewTracker4Error, 800)
                }.duration(50).playOn(v)
            }
        }
    }

    private fun addNewTracker() {
        mViewBinding.apply {
            val deviceOutId = vetAddNewTracker4EditText.content
            if (TextUtils.isEmpty(deviceOutId) || deviceOutId.length < 6) {
                addTrackerError(getString(R.string.txt_add_tracker_tip))
                return
            }
            val intent = Intent(mContext, AddPairedSuccessActivity::class.java)
            intent.putExtra(ConstantString.DeviceOutId, deviceOutId)
            intent.putExtra(ConstantString.isFirstBind, isFirstBind)
            startActivity(intent)
        }
    }

    override fun onClick(v: View?) {
        mViewBinding.apply {
            when (v!!) {
                btnAddNewTrackerAdd -> addNewTracker()
            }
        }
    }
}