package com.pan_american.android.ui.resource_management.hotel_resource

import android.os.Bundle
import android.text.InputType
import android.view.View
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import com.pan_american.android.OASystem
import com.pan_american.android.R
import com.pan_american.android.base.BaseActivity
import com.pan_american.android.base.BaseResponse
import com.pan_american.android.base.ListAdapter
import com.pan_american.android.data.model.resource_management.hotel_resource.entity.HotelResource
import com.pan_american.android.data.network.APIService
import com.pan_american.android.data.network.ServiceCreator
import com.pan_american.android.databinding.ActivityAddHotelResourceBinding
import com.pan_american.android.databinding.LayoutTitleBinding
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class AddHotelResourceActivity : BaseActivity<ActivityAddHotelResourceBinding>() {

    private lateinit var titleBinding: LayoutTitleBinding

    private lateinit var hotelResource: HotelResource

    private var isChose = false

    private var fromList = false

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

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

        fromList = intent.getBooleanExtra("fromList", false)

        initTitle()
        initViews()
        initEvents()
    }

    override fun initTitle() {
        titleBinding = LayoutTitleBinding.bind(binding.root).apply {
            if (fromList) {
                titleText.text = resources.getString(R.string.hotel_resource_detail)
            } else {
                titleText.text = resources.getString(R.string.add_hotel_resource)
            }

            backButton.setOnClickListener {
                back()
            }
        }
    }

    override fun initViews() {

        titleBinding.apply {
            if (fromList) {

                hotelResource = intent.getSerializableExtra("hotelResource") as HotelResource
                initHotelResourceInfo()

                binding.commit.text = resources.getString(R.string.update)

            } else {

                binding.commit.text = resources.getString(R.string.add)

                hotelResource = HotelResource(
                    "", "", "", "", "", "", "", "", "", OASystem.userInfo.userId, ""
                )
            }
        }

    }

    override fun initEvents() {

        binding.hotelLevel.setOnClickListener {
            showSelector {
                val adapter = ListAdapter.Builder<String>().setData(OASystem.level)
                    .setLayoutId(R.layout.item_selector).addBindView { itemView, data ->
                        itemView.findViewById<TextView>(R.id.selector_item_name).apply {
                            text = data

                            if (binding.hotelLevel.text == data) {
                                setTextColor(ResourcesCompat.getColor(resources, R.color.text_color_blue, null))
                            } else {
                                setTextColor(ResourcesCompat.getColor(resources, R.color.text_color, null))
                            }
                        }
                    }.create()

                selector.adapter = adapter

                adapter.onRecyclerViewItemClick =
                    object : ListAdapter.OnRecyclerViewItemClick<String> {
                        override fun onItemClick(position: Int) {

                            binding.hotelLevel.text = OASystem.level[position]
                            isChose = true

                            popupWindow.dismiss()
                        }
                    }
            }
        }

        binding.commit.setOnClickListener {
            if (fromList) {
                initHotelResource(2)
                updateHotelResourceInfo()
            } else {
                if (isChose) {
                    initHotelResource(1)
                    updateHotelResourceInfo()
                } else {
                    showMessage(resources.getString(R.string.hotel_level_select_hint))
                }
            }
        }
    }

    private fun initHotelResourceInfo() {
        binding.apply {
            hotelName.setText(hotelResource.name)
            hotelLocation.setText(hotelResource.city)
            hotelAddress.setText(hotelResource.address)
            hotelPhone.setText(hotelResource.tel)
            hotelFax.setText(hotelResource.fax)
            hotelLevel.text = hotelResource.level
            contactsName.setText(hotelResource.contact)
            contactsPhone.setText(hotelResource.contactPhone)
            otherInfo.setText(hotelResource.otherInformation)

            if (!OASystem.authorization(OASystem.HOTEL_RESOURCE, OASystem.EDIT)) {
                hotelName.inputType = InputType.TYPE_NULL
                hotelLocation.inputType = InputType.TYPE_NULL
                hotelAddress.setInputType(InputType.TYPE_NULL)
                hotelPhone.inputType = InputType.TYPE_NULL
                hotelFax.inputType = InputType.TYPE_NULL
                hotelLevel.isEnabled = false
                contactsName.inputType = InputType.TYPE_NULL
                contactsPhone.inputType = InputType.TYPE_NULL
                otherInfo.setInputType(InputType.TYPE_NULL)
                otherInfo.setSingleLine(false)

                commit.visibility = View.GONE
            }
        }
    }

    private fun initHotelResource(type: Int) {
        hotelResource.apply {
            name = binding.hotelName.text.toString()
            city = binding.hotelLocation.text.toString()
            address = binding.hotelAddress.getText()
            tel = binding.hotelPhone.text.toString()
            fax = binding.hotelFax.text.toString()
            level = binding.hotelLevel.text.toString()
            contact = binding.contactsName.text.toString()
            contactPhone = binding.contactsPhone.text.toString()
            otherInformation = binding.otherInfo.getText()

            status = type
        }
    }

    private fun updateHotelResourceInfo() {

        binding.commit.isEnabled = false

        binding.commit.visibility = View.GONE

        binding.updateLoading.visibility = View.VISIBLE

        val apiService = ServiceCreator.create<APIService>()

        apiService.hotelDataOperation(hotelResource).enqueue(object : Callback<BaseResponse> {
            override fun onResponse(call: Call<BaseResponse>, response: Response<BaseResponse>) {
                val baseResponse = response.body()
                if (baseResponse != null) {
                    if (baseResponse.code == 200) {
                        if (hotelResource.status == 1) {
                            showMessage(resources.getString(R.string.insert_success))
                        } else {
                            showMessage(resources.getString(R.string.update_success))
                        }

                        OASystem.needRefresh = true

                        back()

                    } else {
                        showMessage(baseResponse.msg)

                        binding.commit.isEnabled = true

                        binding.commit.visibility = View.VISIBLE

                        binding.updateLoading.visibility = View.GONE
                    }
                }
            }

            override fun onFailure(call: Call<BaseResponse>, t: Throwable) {

                binding.commit.isEnabled = true

                binding.commit.visibility = View.VISIBLE

                binding.updateLoading.visibility = View.GONE

                showMessage(resources.getString(R.string.network_error))
            }
        })
    }
}