package com.pan_american.android.ui.resource_management.airplane_three_code

import android.os.Bundle
import android.view.View
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.data.model.resource_management.airplane_three_code.entity.AirplaneThreeCode
import com.pan_american.android.data.network.APIService
import com.pan_american.android.data.network.ServiceCreator
import com.pan_american.android.databinding.ActivityAddAirplaneThreeCodeBinding
import com.pan_american.android.databinding.LayoutTitleBinding
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class AddAirplaneThreeCodeActivity : BaseActivity<ActivityAddAirplaneThreeCodeBinding>() {

    private lateinit var titleBinding: LayoutTitleBinding

    private lateinit var airplaneThreeCode: AirplaneThreeCode

    private var fromList = false

    override fun getViewBinding() = ActivityAddAirplaneThreeCodeBinding.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.three_code_detail)
            } else {
                titleText.text = resources.getString(R.string.add_three_code)
            }

            backButton.setOnClickListener {
                back()
            }
        }
    }

    override fun initViews() {

        if (fromList) {

            airplaneThreeCode =
                intent.getSerializableExtra("airplaneResource") as AirplaneThreeCode
            initAirplaneResourceInfo()

            binding.commit.setText(resources.getString(R.string.update))
        } else {

            binding.commit.setText(resources.getString(R.string.add))

            airplaneThreeCode = AirplaneThreeCode()
        }
    }

    override fun initEvents() {

        binding.commit.setOnClickListener {
            if (intent.getBooleanExtra("fromList", false)) {
                initAirplaneResource(2)
                updateAirplaneResource()
            } else {
                initAirplaneResource(1)
                updateAirplaneResource()
            }
        }
    }

    private fun initAirplaneResourceInfo() {
        binding.apply {
            threeCode.setText(airplaneThreeCode.three)
            fourCode.setText(airplaneThreeCode.four)
            airplaneCountry.setText(airplaneThreeCode.country)
            airplaneCity.setText(airplaneThreeCode.city)
            airplaneName.setText(airplaneThreeCode.airPort)
            airplaneNameEn.setText(airplaneThreeCode.airPort_En)
            otherInfo.setText(airplaneThreeCode.remark)

            if (!OASystem.authorization(OASystem.THREE_CODE_RESOURCE, OASystem.EDIT)) {
                threeCode.isEnabled = false
                fourCode.isEnabled = false
                airplaneCountry.isEnabled = false
                airplaneCity.isEnabled = false
                airplaneName.isEnabled(false)
                airplaneNameEn.isEnabled(false)
                otherInfo.isEnabled(false)

                commit.visibility = View.GONE
            }
        }
    }

    private fun initAirplaneResource(type: Int) {
        airplaneThreeCode.apply {
            three = binding.threeCode.text.toString()
            four = binding.fourCode.text.toString()
            country = binding.airplaneCountry.text.toString()
            city = binding.airplaneCity.text.toString()
            airPort = binding.airplaneName.getText()
            airPort_En = binding.airplaneNameEn.getText()
            remark = binding.otherInfo.getText()

            status = type
        }
    }

    private fun updateAirplaneResource() {

        binding.commit.setButtonStatus(1)

        val apiService = ServiceCreator.create<APIService>()

        apiService.airplaneThreeCodeOperation(airplaneThreeCode)
            .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 (airplaneThreeCode.status == 1) {
                                showMessage(resources.getString(R.string.insert_success))
                            } else {
                                showMessage(resources.getString(R.string.update_success))
                            }

                            OASystem.needRefresh = true

                            back()

                        } else {

                            binding.commit.setButtonStatus(0)

                            showMessage(baseResponse.msg)
                        }
                    }
                }

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

                    binding.commit.setButtonStatus(0)

                    showErrorInfo(R.string.update_error)
                }
            })
    }
}