package com.pan_american.android.ui.group_management.entry_and_exit_fee_detail

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.data.model.group_management.entry_and_exit_fee_detail.network.EntryAndExitDetailRequest
import com.pan_american.android.data.model.group_management.entry_and_exit_fee_detail.network.EntryAndExitDetailResponse
import com.pan_american.android.data.network.APIService
import com.pan_american.android.data.network.ServiceCreator
import com.pan_american.android.databinding.ActivityEntryAndExitDetailBinding
import com.pan_american.android.databinding.LayoutTitleBinding
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class EntryAndExitDetailActivity : BaseActivity<ActivityEntryAndExitDetailBinding>() {

    private var groupId = 0

    private lateinit var titleBinding: LayoutTitleBinding

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

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

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

        intent.apply {
            groupId = getIntExtra("groupId", 0)
        }

        initTitle()

        getTotalPaymentInfo()
    }

    override fun initTitle() {
        titleBinding = LayoutTitleBinding.bind(binding.root).apply {
            titleText.text = resources.getString(R.string.entry_and_exit_fee_detail)

            backButton.setOnClickListener {
                back()
            }
        }
    }

    override fun initViews() {
        supportFragmentManager.beginTransaction().addToBackStack(null)
            .add(binding.entryAndExitDetailContainer.id, EntryAndExitFeeTotalFragment()).commit()
    }

    private fun getTotalPaymentInfo() {
        apiService.getEntryAndExitPaymentDetail(EntryAndExitDetailRequest(groupId)).enqueue(object : Callback<EntryAndExitDetailResponse> {
            override fun onResponse(
                call: Call<EntryAndExitDetailResponse>,
                response: Response<EntryAndExitDetailResponse>
            ) {
                val detailResponse = response.body()

                if (detailResponse != null) {

                    OASystem.entryAndExitDetailResponse = detailResponse

                    initViews()
                }
            }

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