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

import android.os.Bundle
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.model.resource_management.airplane_three_code.network.AirplaneThreeCodeDetailRequest
import com.pan_american.android.data.model.resource_management.airplane_three_code.network.AirplaneThreeCodeDetailResponse
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 com.pan_american.android.util.CommitButton
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class AddAirplaneThreeCodeActivity : BaseActivity<ActivityAddAirplaneThreeCodeBinding>() {

    private lateinit var titleBinding: LayoutTitleBinding

    private var threeCodeId = 0

    private val apiService = ServiceCreator.create<APIService>()

    private val airplaneThreeCode = AirplaneThreeCode()

    private var fromList = false

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

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

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

            if (fromList) {
                threeCodeId = getIntExtra("id", 0)
            }
        }

        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) {

            getAirplaneThreeCodeById()

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

        } else {

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

    override fun initEvents() {

        binding.commit.setOnClickListener {

            if (!OASystem.authorization(OASystem.THREE_CODE_RESOURCE, OASystem.EDIT)) {
                back()
                return@setOnClickListener
            }

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

    private fun getAirplaneThreeCodeById() {

        apiService.getAirplaneThreeCodeDetailById(AirplaneThreeCodeDetailRequest(threeCodeId)).enqueue(object : Callback<AirplaneThreeCodeDetailResponse> {
            override fun onResponse(
                call: Call<AirplaneThreeCodeDetailResponse>,
                response: Response<AirplaneThreeCodeDetailResponse>
            ) {
                val detailResponse = response.body()

                if (detailResponse != null) {
                    if (detailResponse.code == 200) {
                        airplaneThreeCode.apply {
                            id = threeCodeId
                            three = detailResponse.data.three
                            four = detailResponse.data.four
                            airPort = detailResponse.data.airPort
                            airPort_En = detailResponse.data.airPort_En
                            city = detailResponse.data.city
                            remark = detailResponse.data.remark
                        }

                        initAirplaneResourceInfo()
                    } else {
                        showMessage(detailResponse.msg)
                    }
                }
            }

            override fun onFailure(p0: Call<AirplaneThreeCodeDetailResponse>, p1: Throwable) {
                showErrorInfo(R.string.airplane_three_code_detail_get_failed)
            }
        })
    }

    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.setText(resources.getString(R.string.back))
            }
        }
    }

    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(CommitButton.STATUS_IN_COMMITING)

        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(CommitButton.STATUS_COMMITTED)

                            showMessage(baseResponse.msg)
                        }
                    }
                }

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

                    binding.commit.setButtonStatus(CommitButton.STATUS_COMMITTED)

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