package com.dlc.eboard.ui.person

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.dlc.eboard.R
import com.dlc.eboard.common.CommonUtils
import com.dlc.eboard.databinding.FragmentLabPersonAccessBinding
import com.dlc.eboard.sp.DeviceSp
import com.rc.core.ui.fragment.RcRefreshFragment
import com.rc.core.ui.widget.decoration.GridSpacingItemDecoration
import com.rc.core.util.DeviceUtils
import com.rc.core.util.ifNullOrEmpty
import com.rc.httpcore.client.ApiRepository
import com.rc.httpcore.vo.request.OnLineUserReq
import com.rc.httpcore.vo.response.LabPersonVo
import io.reactivex.disposables.Disposable

/**
 * 人员信息-实验人员 type=1
 * 人员信息-准入人员 type=2
 *
 * @author ReiChin_
 */
class LabAccessPersonFragment :
    RcRefreshFragment<LabPersonVo, FragmentLabPersonAccessBinding>() {

    override val mSrlRefresh: SwipeRefreshLayout by lazy { viewBinding.swipeRefreshLayout }
    override val mRvContent: RecyclerView by lazy { viewBinding.recyclerView }
    override val mAdapter: BaseQuickAdapter<LabPersonVo, BaseViewHolder>
            by lazy { LabAccessPersonAdapter(mType) }

    private var mType: Int = TYPE_EXPERIMENT // 1-实验人员 2-准入人员
    private lateinit var mLabId: String

    companion object {
        const val TYPE_EXPERIMENT = 1 // 实验人员
        const val TYPE_ACCESS = 2 // 准入人员

        fun newInstance(type: Int, labId: String): LabAccessPersonFragment {
            val args = Bundle()
            args.putInt("type", type)
            args.putString("labId", labId)
            val fragment = LabAccessPersonFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun createViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentLabPersonAccessBinding {
        return FragmentLabPersonAccessBinding.inflate(inflater, container, false)
    }

    override fun initViews(savedInstanceState: Bundle?) {
        mType = requireArguments().getInt("type")
        mLabId = requireArguments().getString("labId")!!
        super.initViews(savedInstanceState)
    }

    override fun createLayoutManager(): RecyclerView.LayoutManager {
        return GridLayoutManager(requireContext(), 4)
    }

    override fun createItemDecoration(): RecyclerView.ItemDecoration {
        return GridSpacingItemDecoration(
            4,
            DeviceUtils.dip2px(requireContext(), 22f),
            DeviceUtils.dip2px(requireContext(), 12f), false
        )
    }

    override fun queryData(): Disposable? {
        return ApiRepository.queryAccessPerson(mType, mLabId, mCurrentPage, PAGE_SIZE)
            .subscribe({ data ->
                dispatchLoadDataSuccess(data)
            }, { throwable ->
                dispatchLoadDataFailure(throwable)
            })
    }

}

// type 1-实验人员 2-准入人员
private class LabAccessPersonAdapter(private val type: Int) :
    BaseQuickAdapter<LabPersonVo, BaseViewHolder>(R.layout.item_lab_person_access),
    LoadMoreModule {

    override fun convert(holder: BaseViewHolder, item: LabPersonVo) {
        // 头像
        Glide.with(context)
            .load(item.avatar)
            .placeholder(R.mipmap.icon_sign_in_avatar)
            .error(R.mipmap.icon_sign_in_avatar)
//            .transform(RoundedCorners(DeviceUtils.dip2px(context, 6f)))
            .into(holder.getView(R.id.pPhoto))

        holder.setText(R.id.pName, "姓名：${item.userName.ifNullOrEmpty()}")
        if (1 == type) {
            // 1-实验人员
            // 学号(工号)：1234567
            // 签到时间：01-05 14:56
            holder.setText(R.id.pCardNumber, "学号(工号): ${item.jobNumber.ifNullOrEmpty()}")
            holder.setText(R.id.pTime, "签到时间: ${formatSignTime(item.signTime)}")
        } else {
            // 2-准入人员  准入期限：24-10-10
            // 学号：1234567
            // 准入期限: 23-10-10至24-10-10
            holder.setText(R.id.pCardNumber, "学号(工号): ${item.jobNumber.ifNullOrEmpty()}")
            holder.setText(R.id.pTime, "准入期限: ${formatValidTime(item.validBeginTime)}至${formatValidTime(item.validEndTime)}")
        }
    }

    private fun formatValidTime(input: String?): String {
        // 2023-10-10 => 23-10-10
        return if (input.isNullOrEmpty()) "" else input.substring(2, input.length)
    }

    private fun formatSignTime(time: String?): CharSequence {
        // 2023-03-30 15:45:25 => 03-30 15:45
        return if (time.isNullOrEmpty()) "" else time.substring(5, 16)
    }

}