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.ListAdapter
import com.pan_american.android.data.model.address_book.entity.Member
import com.pan_american.android.data.model.address_book.network.MemberListRequest
import com.pan_american.android.data.model.address_book.network.MemberListResponse
import com.pan_american.android.data.network.APIService
import com.pan_american.android.data.network.ServiceCreator
import com.pan_american.android.databinding.FragmentMemberListBinding
import org.greenrobot.eventbus.EventBus
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MemberListFragment : BaseFragment<FragmentMemberListBinding>() {

    private var memberList = ArrayList<Member>()

    private var listInit = false

    private var companyId = 0

    private var companyName = ""

    private var depId = 0

    private var depName = ""

    override fun getViewBinding(
        inflater: LayoutInflater, container: ViewGroup?, bundle: Bundle?
    ) = FragmentMemberListBinding.inflate(layoutInflater, container, false)

    override fun onStart() {
        super.onStart()

        requireArguments().apply {
            companyId = getInt("companyId")
            companyName = getString("companyName").toString()
            depId = getInt("depId")
            depName = getString("depName").toString()
        }

        initViews()
    }

    override fun initViews() {
        binding.subtitle.text =
            String.format(resources.getString(R.string.member_sub_title, companyName, depName))
        initMemberList()
    }

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

        if (!listInit) {
            ServiceCreator.create<APIService>().getMembersList(MemberListRequest(companyId, depId))
                .enqueue(object : Callback<MemberListResponse> {
                    override fun onResponse(
                        call: Call<MemberListResponse>, response: Response<MemberListResponse>
                    ) {
                        val memberListResponse = response.body()
                        if (memberListResponse != null) {
                            if (memberListResponse.code == 200) {
                                for (item in memberListResponse.data) {
                                    memberList.add(item)
                                }
                                initList()
                                listInit = true
                            } else {
                                showMessage(memberListResponse.msg)
                            }
                        }
                    }

                    override fun onFailure(call: Call<MemberListResponse>, t: Throwable) {
                        showMessage(resources.getString(R.string.network_error))
                    }
                })
        } else {
            initList()
        }
    }

    private fun initList() {
        val adapter = ListAdapter.Builder<Member>().apply {
            setData(memberList)
            setLayoutId(R.layout.item_members_list)
            addBindView { itemView, data ->
                itemView.findViewById<TextView>(R.id.member_name).text = data.cnName
                itemView.findViewById<TextView>(R.id.member_job).text = data.jobName
            }
        }.create()

        val layoutManager = LinearLayoutManager(OASystem.context)

        binding.membersListView.layoutManager = layoutManager

        binding.membersListView.adapter = adapter

        adapter.onRecyclerViewItemClick = object : ListAdapter.OnRecyclerViewItemClick<Member> {
            override fun onItemClick(position: Int) {
                val member = memberList[position]
                EventBus.getDefault().post(member)
            }
        }
    }

}