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

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
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 companyId = 0

    private var companyName = ""

    private var departmentId = 0

    private var departmentName = ""

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

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

        initViews()
    }

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

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

        ServiceCreator.create<APIService>().getMembersList(MemberListRequest(companyId, departmentId))
            .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()

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

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

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

}