package com.pan_american.android.ui.group_management.group_info

import android.content.Intent
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.BaseRequest
import com.pan_american.android.data.model.group_management.group_info.network.GroupInfoBaseDataResponse
import com.pan_american.android.data.network.APIService
import com.pan_american.android.data.network.ServiceCreator
import com.pan_american.android.databinding.ActivityGroupInfoBinding
import com.pan_american.android.databinding.LayoutTitleBinding
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class GroupInfoActivity : BaseActivity<ActivityGroupInfoBinding>() {

    private lateinit var titleBinding: LayoutTitleBinding

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

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

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

        getGroupInfoBaseData()

        initTitle()
    }

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

            if (OASystem.authorization(OASystem.GROUP_INFORMATION, OASystem.ADD)) {
                addButton.apply {

                    visibility = View.VISIBLE

                    setOnClickListener {
                        val intent = Intent(OASystem.context, AddGroupInfoActivity::class.java)
                        startActivity(intent)
                    }
                }
            }

            backButton.setOnClickListener {
                back()
            }
        }
    }

    private fun getGroupInfoBaseData() {

        apiService.getGroupInfoBaseData(BaseRequest())
            .enqueue(object : Callback<GroupInfoBaseDataResponse> {
                override fun onResponse(
                    call: Call<GroupInfoBaseDataResponse>,
                    response: Response<GroupInfoBaseDataResponse>
                ) {
                    val baseDataResponse = response.body()

                    if (baseDataResponse != null) {
                        if (baseDataResponse.code == 200) {

                            OASystem.groupInfoGroupType = baseDataResponse.data.teamTypeData

                            OASystem.groupInfoCustomerLevel = baseDataResponse.data.teamLevData

                            OASystem.groupInfoTradeDirector = baseDataResponse.data.userData

                            supportFragmentManager.beginTransaction().addToBackStack(null)
                                .add(binding.groupInformationContainer.id, GroupInfoSearchFragment())
                                .commit()

                        } else {
                            showMessage(baseDataResponse.msg)
                        }
                    }
                }

                override fun onFailure(call: Call<GroupInfoBaseDataResponse>, t: Throwable) {
                    showErrorInfo(R.string.base_resource_data_get_failed)
                }
            })
    }
}