package com.pan_american.android.ui.efficiency_tools.address_book

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.pan_american.android.OASystem
import com.pan_american.android.R
import com.pan_american.android.base.BaseFragment
import com.pan_american.android.base.BaseRequest
import com.pan_american.android.base.ListAdapter
import com.pan_american.android.data.model.address_book.entity.Company
import com.pan_american.android.data.model.address_book.network.CompanyListResponse
import com.pan_american.android.data.network.APIService
import com.pan_american.android.data.network.ServiceCreator
import com.pan_american.android.databinding.FragmentCompanyListBinding
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class CompanyListFragment : BaseFragment<FragmentCompanyListBinding>() {

    private val companyList = ArrayList<Company>()

    private var listInit = false

    override fun getViewBinding(

        inflater: LayoutInflater, container: ViewGroup?, bundle: Bundle?
    ) = FragmentCompanyListBinding.inflate(layoutInflater, container, false)

    override fun onStart() {
        super.onStart()

        initViews()
    }

    override fun initViews() {
        initCompanyList()
    }

    private fun initCompanyList() {

        //避免Fragment not attached to a context.
        if (!isAdded) {
            return
        }

        if (!listInit) {
            ServiceCreator.create<APIService>().getCompanyList(BaseRequest())
                .enqueue(object : Callback<CompanyListResponse> {
                    override fun onResponse(
                        call: Call<CompanyListResponse>, response: Response<CompanyListResponse>
                    ) {
                        val companyListResponse = response.body()
                        if (companyListResponse != null) {
                            if (companyListResponse.code == 200) {
                                for (item in companyListResponse.data) {
                                    companyList.add(item)
                                }
                                initList()
                                listInit = true
                                binding.totalText.text = String.format(
                                    resources.getString(
                                        R.string.company_total, companyList.size
                                    )
                                )
                            } else {
                                showMessage(companyListResponse.msg)
                            }
                        }
                    }

                    override fun onFailure(call: Call<CompanyListResponse>, t: Throwable) {
                        showErrorInfo(R.string.company_get_failed)
                    }
                })
        } else {
            initList()
        }
    }

    private fun initList() {
        val adapter = ListAdapter.Builder<Company>().apply {
            setData(companyList)
            setLayoutId(R.layout.item_company_list)
            addBindView { itemView, data ->
                itemView.findViewById<TextView>(R.id.selector_item_name).text = data.companyName
            }
        }.create()

        val layoutManager = LinearLayoutManager(OASystem.context)

        binding.companyListView.layoutManager = layoutManager

        binding.companyListView.adapter = adapter

        adapter.onRecyclerViewItemClick = object : ListAdapter.OnRecyclerViewItemClick<Company> {
            override fun onItemClick(position: Int) {
                val companyId = companyList[position].id
                val companyName = companyList[position].companyName

                val bundle = Bundle().apply {
                    putInt("companyId", companyId)
                    putString("companyName", companyName)
                }

                val departmentListFragment = DepartmentListFragment()
                departmentListFragment.arguments = bundle

                parentFragmentManager.beginTransaction().setCustomAnimations(
                        R.anim.slide_right_in,
                        R.anim.slide_left_out,
                        R.anim.slide_left_in,
                        R.anim.slide_right_out
                    ).addToBackStack(null)
                    .replace(R.id.address_book_container, departmentListFragment).commit()
            }
        }
    }
}