package com.yidianling.medical.archives

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.ydl.ydlcommon.base.BaseActivity
import com.ydl.ydlcommon.utils.StatusBarUtils
import com.yidianling.im.R
import com.yidianling.im.bean.SetCourseStatus
import com.yidianling.medical.archives.adapter.MedicalUserArchivesAdapter
import com.yidianling.medical.widgets.LoadMoreView
import com.yidianling.uikit.custom.http.ServiceImpl.Companion.instance
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.medical_archives_list_activity.*

/**
 * 用户档案列表页面
 * */
class MedicalArchivesListActivity : BaseActivity(), SwipeRefreshLayout.OnRefreshListener,
    BaseQuickAdapter.RequestLoadMoreListener {

    companion object {
        private var currPage: Int = 1
        private val pageSize: Int = 20
        private lateinit var mEmptyView: View
        private lateinit var mAdapter: MedicalUserArchivesAdapter
        private var isFirstLoadData = true

        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, MedicalArchivesListActivity::class.java)
            context.startActivity(starter)
        }
    }

    override fun layoutResId(): Int {
        return R.layout.medical_archives_list_activity
    }

    override fun initDataAndEvent() {
        initStatus()
        mEmptyView =
            LayoutInflater.from(this).inflate(R.layout.medical_list_empty_view, null, false)
        var reLoad = mEmptyView.findViewById<View>(R.id.reLoad)
        reLoad.setOnClickListener {
            onRefresh()
        }
        mAdapter = MedicalUserArchivesAdapter()
        rv_archives_list.layoutManager = LinearLayoutManager(
            this@MedicalArchivesListActivity,
            LinearLayoutManager.VERTICAL,
            false
        );
        rv_archives_list.adapter = mAdapter
        mAdapter.setLoadMoreView(LoadMoreView())
        mAdapter.setOnLoadMoreListener(this@MedicalArchivesListActivity, rv_archives_list)
        doctor_swipe_refresh_layout?.setOnRefreshListener(this)
        doctor_swipe_refresh_layout?.setColorSchemeColors(
            ContextCompat.getColor(this, R.color.platform_main_theme)
        )
        doctor_swipe_refresh_layout?.setProgressViewOffset(false, 0, 200)

        mAdapter.setOnItemClickListener { baseQuickAdapter, view, position ->
            isFirstLoadData = false
            mAdapter.data[position]?.let {
                MedicalEditArchivesActivity.start(
                    this@MedicalArchivesListActivity,
                    it
                )
            }
        }

        add_archives.setOnClickListener {
            isFirstLoadData = false
            //添加用户档案
            MedicalAddNewArchivesActivity.start(this@MedicalArchivesListActivity)
        }
        iv_back.setOnClickListener {
            finish()
        }

        currPage = 1
        getPageList()
    }

    override fun onResume() {
        super.onResume()
        if (!isFirstLoadData) {
            getPageList()
        }
    }

    @SuppressLint("CheckResult")
    private fun getPageList() {
        instance.getPageList(currPage, pageSize)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({ response ->
                if  ("200" == response.code) {
                    doctor_swipe_refresh_layout.isRefreshing = false

                    var list = response.data.rows

                    if (currPage == 1) {
                        //第一页数据
                        if (list != null && list.isNotEmpty()) {
                            if (list.size < pageSize) {
                                mAdapter.setNewData(list)
                                mAdapter.removeAllFooterView()
                                mAdapter.loadMoreEnd(false)
                            } else {
                                currPage++
                                mAdapter.setNewData(list)
                                mAdapter.removeAllFooterView()
                                mAdapter.loadMoreComplete()
                            }
                        } else {
                            mAdapter.loadMoreEnd(false)
                            mAdapter.setNewData(list)
                            var net_error= mEmptyView.findViewById<TextView>(R.id.net_error)
                            net_error.text="暂无就诊人信息，请添加～"
                            mAdapter.emptyView = mEmptyView
                        }
                    } else {
                        //非第一页数据
                        if (list != null && list.isEmpty()) {
                            mAdapter.loadMoreEnd(false)
                        } else {
                            if (list.size < pageSize) {
                                mAdapter.addData(list)
                                mAdapter.loadMoreEnd(false)
                            } else {
                                mAdapter.addData(list)

                                if (currPage == response.data.totalPage) {
                                    mAdapter.loadMoreEnd(false)
                                } else {
                                    currPage++
                                    mAdapter.loadMoreComplete()
                                }
                            }
                        }
                    }
                } else {
                    doctor_swipe_refresh_layout.isRefreshing = false
                    mAdapter.loadMoreFail()

                    mAdapter.emptyView = mEmptyView
                }
            }, { e ->
                doctor_swipe_refresh_layout.isRefreshing = false
                mAdapter.loadMoreFail()
                var net_error= mEmptyView.findViewById<TextView>(R.id.net_error)
                var reLoad= mEmptyView.findViewById<TextView>(R.id.reLoad)
                net_error.text="网络异常，请稍后再试"
                reLoad.visibility=View.VISIBLE
                mAdapter.emptyView = mEmptyView

            })
    }

    override fun onRefresh() {
        currPage = 1
        getPageList()

    }

    override fun onLoadMoreRequested() {
        getPageList()
    }

    private fun initStatus() {
        StatusBarUtils.setTransparentForImageView(this@MedicalArchivesListActivity, null)
        StatusBarUtils.statusBarLightMode(this@MedicalArchivesListActivity)
    }

}