package com.pan_american.android.ui.efficiency_tools.address_book

import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.TextView
import com.pan_american.android.R
import com.pan_american.android.base.BaseActivity
import com.pan_american.android.data.model.address_book.entity.Member
import com.pan_american.android.databinding.ActivityAddressBookBinding
import com.pan_american.android.databinding.LayoutTitleBinding
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class AddressBookActivity : BaseActivity<ActivityAddressBookBinding>() {

    private lateinit var titleBinding: LayoutTitleBinding

    override fun getViewBinding() = ActivityAddressBookBinding.inflate(layoutInflater)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        screenAdaptation(binding)

        initTitle()
        initViews()
    }

    override fun onStart() {
        super.onStart()

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }

    }

    override fun onStop() {
        super.onStop()

        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }

    override fun initTitle() {

        titleBinding = LayoutTitleBinding.bind(binding.root).apply {
            titleText.text = resources.getString(R.string.address_book)
            searchView.visibility = View.VISIBLE
            backButton.setOnClickListener {
                back()
            }
        }

    }

    override fun initViews() {
        supportFragmentManager.beginTransaction().addToBackStack(null)
            .replace(binding.addressBookContainer.id, CompanyListFragment()).commit()
    }

    //员工界面PopupWindow展示
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun showMemberPopupWindow(member: Member) {

        val popView = View.inflate(this, R.layout.popup_member_info, null)
        popupWindow = PopupWindow(
            popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
        )

        showPopupWindow {

            val memberName: TextView = popView.findViewById(R.id.member_name)
            val jobName: TextView = popView.findViewById(R.id.member_job)
            val phoneNumber: TextView = popView.findViewById(R.id.phone_number)
            val phoneNumberEmergency: TextView = popView.findViewById(R.id.phone_number_emergency)
            val email: TextView = popView.findViewById(R.id.mail_address)

            memberName.text = member.cnName
            jobName.text = member.jobName
            phoneNumber.text = member.phone.ifBlank {
                resources.getString(R.string.no_info)
            }
            phoneNumberEmergency.text = member.urgentPhone.ifBlank {
                resources.getString(R.string.no_info)
            }
            email.text = member.email.ifBlank {
                resources.getString(R.string.no_info)
            }

            popupWindow.showAtLocation(binding.root, Gravity.BOTTOM, 0, 0)
        }
    }
}