package com.base.locationsharewhite.ui.main

import android.annotation.SuppressLint
import android.graphics.Color
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import com.base.locationsharewhite.R
import com.base.locationsharewhite.databinding.ActivityLocationShareBinding
import com.base.locationsharewhite.helper.BaseActivity
import com.base.locationsharewhite.location.LocationShareListUtils
import com.base.locationsharewhite.location.LocationShareUtils
import com.base.locationsharewhite.location.LocationShareUtils.SHARE_STATE_FAILED
import com.base.locationsharewhite.location.LocationShareUtils.SHARE_STATE_MYSELF
import com.base.locationsharewhite.location.LocationShareUtils.SHARE_STATE_SHARED
import com.base.locationsharewhite.location.LocationShareUtils.SHARE_STATE_SUCCESS
import com.base.locationsharewhite.location.LocationStatusUtils
import com.base.locationsharewhite.utils.BarUtils
import com.base.locationsharewhite.utils.LogEx
import com.base.locationsharewhite.utils.ToastUtils.toast

/**
 * 添加谁可以看我
 */
class LocationShareActivity : BaseActivity<ActivityLocationShareBinding>() {
    private val TAG = "LocationShareActivity"

    override val binding: ActivityLocationShareBinding by lazy {
        ActivityLocationShareBinding.inflate(layoutInflater)
    }
    private lateinit var adapter: ViewerAdapter


    @SuppressLint("NotifyDataSetChanged")
    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.WHITE)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())


        binding.editCode.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                binding.editCode.hint = ""
            } else {
                binding.editCode.hint = this.resources.getString(R.string.enter_others_location_code)
            }
        }

        binding.shimmerLayout.startShimmerAnimation()
        adapter = ViewerAdapter(switchAction = { item, isChecked ->
            val changeStatus = if (isChecked) 1 else 0
            LogEx.logDebug(TAG, "device=${item.device}")
            LocationStatusUtils.changeViewerStatus(item.device, changeStatus) { flag ->
                runOnUiThread {
                    if (flag) {
                        item.status = changeStatus
                    }
                    adapter.notifyDataSetChanged()
                }
            }
        })
        binding.rv.adapter = adapter


        initShareData()
    }


    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            finish()
        }
        binding.flFanhui.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.tvShare.setOnClickListener {
            val code = binding.editCode.text.toString()
            if (code.length >= 8) {
                LocationShareUtils.shareMyLocationByInvitationCode(code) { status ->
                    runOnUiThread {
                        when (status) {
                            SHARE_STATE_FAILED -> {
                                toast("Code verification failed")
                            }

                            SHARE_STATE_SUCCESS -> {
                                toast("Code verification success")
                            }

                            SHARE_STATE_SHARED -> {
                                toast("Sharing code has been added")
                            }

                            SHARE_STATE_MYSELF -> {
                                toast("Sharing code is yourself code")
                            }
                        }

                    }
                }
            } else {
                toast("code verification failed")
            }
        }
    }

    private fun initShareData() {
        LocationShareListUtils.getShareList(viewerListCallBack = {
            runOnUiThread {
                adapter.submitList(it)
            }
        })
    }


}