package cn.muchinfo.rma.view.base.home.customerdata

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.data.UserInfoData
import cn.muchinfo.rma.lifecycle.bind
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.*
import cn.muchinfo.rma.view.base.BaseActivity
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.ToastUtils
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.tab.QMUITabIndicator
import com.qmuiteam.qmui.widget.tab.QMUITabSegment

import mtp.polymer.com.autowidget.adapter.BaseAdapter
import org.jetbrains.anko.*
import org.jetbrains.anko.support.v4.viewPager

/**
 * 客户资料列表页
 */
class CustomerDataActivity : BaseActivity<CustomerDataViewModel>() {

    lateinit var segment: QMUITabSegment
    var selectedTabIndex: Int = 0
    private lateinit var viewPager: ViewPager

    private val tabsArray: List<String> by lazy {
        arrayListOf(
            resources.getString(R.string.un_submit),
            resources.getString(R.string.to_audit),
            resources.getString(R.string.normal),
            resources.getString(R.string.disable)
        )
    }

    // 未提交
    private val unSubmitUI: UnSubmitUI by lazy { UnSubmitUI(this, viewModel) }
    // 待审核
    private val toAuditUI: ToAuditUI by lazy { ToAuditUI(this, viewModel) }
    // 正常
    private val normalUI: NormalUI by lazy { NormalUI(this, viewModel) }
    // 停用
    private val disableUI: DisableUI by lazy { DisableUI(this, viewModel) }

    private val pagerAdapter: PagerAdapter by lazy {
        object : PagerAdapter() {

            override fun getPageTitle(position: Int): CharSequence? = tabsArray[position]

            override fun instantiateItem(container: ViewGroup, position: Int): Any {
                when (position) {
                    0 -> {  // 未提交
                        return unSubmitUI.root.apply {
                            container.addView(
                                this,
                                ViewGroup.LayoutParams(matchParent, matchParent)
                            )
                        }
                    }
                    1 -> { // 待审核
                        return toAuditUI.root.apply {
                            container.addView(
                                this,
                                ViewGroup.LayoutParams(matchParent, matchParent)
                            )
                        }
                    }
                    2 -> {
                        return normalUI.root.apply {
                            container.addView(
                                this,
                                ViewGroup.LayoutParams(matchParent, matchParent)
                            )
                        }
                    }
                    3 -> {
                        return disableUI.root.apply {
                            container.addView(
                                this,
                                ViewGroup.LayoutParams(matchParent, matchParent)
                            )
                        }
                    }
                    else -> return super.instantiateItem(container, position)
                }
            }

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
                (`object` as? View)?.let { container.removeView(it) }
            }

            override fun isViewFromObject(view: View, `object`: Any): Boolean = view == `object`

            override fun getCount(): Int = tabsArray.size

        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        verticalLayout {
            // 标题栏
            topBar {
                // 返回
                commonLeftButton()
                commonTitle {
                    textResource = R.string.customer_data
                }
                // 新增客户申请
                commonMenuButton(R.mipmap.customer_add) {
                    val intent = Intent()
                    intent.putExtra("status","1")
                    intent.putExtra("userInfoData",UserInfoData())
                    intent.setClass(context,AddCustomerDataActivity::class.java)
                    ActivityUtils.startActivity(intent)
                }
            }

            // 分页框
            tabSegment {
                segment = this
                background = resources.getDrawable(R.color.segtabment_bg_color)
                addOnTabSelectedListener(object :
                    QMUITabSegment.OnTabSelectedListener {
                    override fun onDoubleTap(index: Int) {}
                    override fun onTabReselected(index: Int) {}
                    override fun onTabUnselected(index: Int) {}
                    override fun onTabSelected(index: Int) {
                        selectedTabIndex = index
                    }
                })
                setIndicator(
                    QMUITabIndicator(
                        QMUIDisplayHelper.dp2px(context, 2),
                        false,
                        true
                    )
                )
                setDefaultTextSize(ConvertUtils.sp2px(16f),ConvertUtils.sp2px(16f))
            }.lparams(matchParent, autoSize(100))

            view {
                background = resources.getDrawable(R.color.main_hit_bg_color)
            }.lparams(matchParent, autoSize(1)) {
                marginStart = autoSize(32)
                marginEnd = autoSize(32)
            }
            // 内容
            viewPager {
                background = resources.getDrawable(R.color.white)
                viewPager = this
                adapter = pagerAdapter
            }.lparams(matchParent, autoSize(2000))

            segment.setupWithViewPager(viewPager)
            segment.selectTab(selectedTabIndex)

        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.queryUserInfo(type = "1")
        viewModel.queryUserInfo(type = "2")
        viewModel.queryUserInfo(type = "3")
        viewModel.queryUserInfo(type = "4")
//        when (selectedTabIndex) {
//            0 -> {
//
//            }
//            1 -> {
//
//            }
//            3 -> {
//
//            }
//            4 -> {
//
//            }
//        }
    }

}


/**
 * 未提交
 */
class UnSubmitUI(
    private val activity: AppCompatActivity,
    private val viewModel: CustomerDataViewModel
) {
    // 下拉刷新
    private lateinit var swipeToLayout: SwipeToLoadLayout
    private lateinit var statusLayout: StatusLayout

    private val unSubmitAdapter: BaseAdapter<UserInfoData, CustomerDataViewHolder> = BaseAdapter { _, _ -> CustomerDataViewHolder(activity, viewModel,"1") }

    init {
        viewModel.queryUserInfo(type = "1")
    }

    val root = _FrameLayout(activity).apply {
        verticalLayout {
            // 标题栏
            linearLayout {
                gravity = Gravity.CENTER_VERTICAL
                textView {
                    textResource = R.string.user_info_type
                    textSizeAuto = 24
                    textColorInt = R.color.hint_text_color
                }.lparams(wrapContent, wrapContent) {
                    marginStart = autoSize(32)
                }

                textView {
                    textResource = R.string.user_info_name
                    textColorInt = R.color.hint_text_color
                    textSizeAuto = 24
                }.lparams(wrapContent, wrapContent){
                    marginStart = autoSize(95)
                }

                textView {
                    textResource = R.string.user_info_phone
                    textColorInt = R.color.hint_text_color
                    textSizeAuto = 24
                }.lparams(wrapContent, wrapContent){
                    marginStart = autoSize(215)
                }

                textView {
                    textResource = R.string.status
                    textColorInt = R.color.hint_text_color
                    textSizeAuto = 24
                }.lparams(wrapContent, wrapContent){
                    marginStart = autoSize(100)
                }

            }.lparams(matchParent, autoSize(68))

            view {
                background = resources.getDrawable(R.color.line_hint_color)
            }.lparams(matchParent, autoSize(1))

            statusLayout(contentBlock = {
                statusLayout = this
//                bindTaskStatus(viewModule.status)
                setRetryAction {
                    viewModel.queryUserInfo(type = "1")
                }
                swipeToLoadLayout {
                    swipeToLayout = this
                    setEnableRefresh(true)
                    setEnableLoadMore(false)
                    setOnRefreshListener {
                        viewModel.queryUserInfo(type = "1")
                    }
                    setEnableScrollContentWhenLoaded(false)
                    setEnableLoadMoreWhenContentNotFull(false)

                    // 未提交列表
                    recyclerView {
                        background = resources.getDrawable(R.color.white)
                        adapter = unSubmitAdapter
                    }.lparams(matchParent, matchParent)
                }
            }, emptyBlock = {
                emptyView(hint = resources.getString(R.string.now_no_data))
            }).lparams(matchParent, matchParent)

            // 绑数据
            viewModel.unSubmitList.bindOptional(context){
                if (it?.isEmpty() == true || it?.size == 0){
                    statusLayout.showEmpty()
                }else {
                    if (swipeToLayout.getIsRefreshing()){
                        swipeToLayout.finishRefresh()
                    }
                    statusLayout.showSuccess()
                    unSubmitAdapter.setNewData(it)
                }
            }
        }
    }
}

/**
 * 待审核
 */
class ToAuditUI(
    private val activity: AppCompatActivity,
    private val viewModel: CustomerDataViewModel
) {

    private lateinit var swipeToLayout: SwipeToLoadLayout
    private lateinit var statusLayout: StatusLayout

    private val toAuditAdapter: BaseAdapter<UserInfoData, CustomerDataViewHolder> = BaseAdapter { _, _ -> CustomerDataViewHolder(activity, viewModel,"2") }

    init {
        viewModel.queryUserInfo(type = "2")
    }

    val root = _FrameLayout(activity).apply {
        verticalLayout {
            linearLayout {
                gravity = Gravity.CENTER_VERTICAL
                textView {
                    textResource = R.string.user_info_type
                    textSizeAuto = 24
                    textColorInt = R.color.hint_text_color
                }.lparams(wrapContent, wrapContent) {
                    marginStart = autoSize(32)
                }

                textView {
                    textResource = R.string.user_info_name
                    textColorInt = R.color.hint_text_color
                    textSizeAuto = 24
                }.lparams(wrapContent, wrapContent){
                    marginStart = autoSize(95)
                }

                textView {
                    textResource = R.string.user_info_phone
                    textColorInt = R.color.hint_text_color
                    textSizeAuto = 24
                }.lparams(wrapContent, wrapContent){
                    marginStart = autoSize(215)
                }

                textView {
                    textResource = R.string.status
                    textColorInt = R.color.hint_text_color
                    textSizeAuto = 24
                }.lparams(wrapContent, wrapContent){
                    marginStart = autoSize(80)
                }

            }.lparams(matchParent, autoSize(68))

            view {
                background = resources.getDrawable(R.color.line_hint_color, null)
            }.lparams(matchParent, autoSize(1))

            statusLayout(contentBlock = {
                statusLayout = this
//                bindTaskStatus(viewModule.status)
                setRetryAction {
                    viewModel.queryUserInfo(type = "2")
                }
                swipeToLoadLayout {
                    swipeToLayout = this
                    setEnableRefresh(true)
                    setEnableLoadMore(false)
                    setOnRefreshListener {
                        viewModel.queryUserInfo(type = "2")
                    }
                    setEnableScrollContentWhenLoaded(false)
                    setEnableLoadMoreWhenContentNotFull(false)
                    recyclerView {
                        background = resources.getDrawable(R.color.white)
                        adapter = toAuditAdapter
                    }.lparams(matchParent, matchParent)
                }
            }, emptyBlock = {
                emptyView(hint = resources.getString(R.string.now_no_data))
            }).lparams(matchParent, matchParent)

            // 绑数据
            viewModel.toAuditList.bindOptional(context){
                if (it?.isEmpty() == true || it?.size == 0){
                    statusLayout.showEmpty()
                }else {
                    if (swipeToLayout.getIsRefreshing()){
                        swipeToLayout.finishRefresh()
                    }
                    statusLayout.showSuccess()
                    toAuditAdapter.setNewData(it)
                }
            }
        }
    }
}


/**
 * 正常
 */
class NormalUI(
    private val activity: AppCompatActivity,
    private val viewModel: CustomerDataViewModel
) {
    private lateinit var swipeToLayout: SwipeToLoadLayout
    private lateinit var statusLayout: StatusLayout

    private val normalAdapter: BaseAdapter<UserInfoData, CustomerDataViewHolder> = BaseAdapter { _, _ -> CustomerDataViewHolder(activity, viewModel,"3") }

    init {
        viewModel.queryUserInfo(type = "3")
    }

    val root = _FrameLayout(activity).apply {
        verticalLayout {
            linearLayout {
                gravity = Gravity.CENTER_VERTICAL
                textView {
                    textResource = R.string.user_info_type
                    textSizeAuto = 24
                    textColorInt = R.color.hint_text_color
                }.lparams(wrapContent, wrapContent) {
                    marginStart = autoSize(32)
                }

                textView {
                    textResource = R.string.user_info_name
                    textColorInt = R.color.hint_text_color
                    textSizeAuto = 24
                }.lparams(wrapContent, wrapContent){
                    marginStart = autoSize(95)
                }

                textView {
                    textResource = R.string.user_info_phone
                    textColorInt = R.color.hint_text_color
                    textSizeAuto = 24
                }.lparams(wrapContent, wrapContent){
                    marginStart = autoSize(215)
                }

                textView {
                    textResource = R.string.status
                    textColorInt = R.color.hint_text_color
                    textSizeAuto = 24
                }.lparams(wrapContent, wrapContent){
                    marginStart = autoSize(80)
                }

            }.lparams(matchParent, autoSize(68))

            view {
                background = resources.getDrawable(R.color.line_hint_color)
            }.lparams(matchParent, autoSize(1))

            statusLayout(contentBlock = {
                statusLayout = this
                setRetryAction {
                    viewModel.queryUserInfo(type = "3")
                }
//                bindTaskStatus(viewModule.status)
                swipeToLoadLayout {
                    swipeToLayout = this
                    setEnableRefresh(true)
                    setEnableLoadMore(false)
                    setOnRefreshListener {
                        viewModel.queryUserInfo(type = "3")
                    }
                    setEnableScrollContentWhenLoaded(false)
                    setEnableLoadMoreWhenContentNotFull(false)
                    recyclerView {
                        background = resources.getDrawable(R.color.white)
                        adapter = normalAdapter
                    }.lparams(matchParent, matchParent)
                }
            }, emptyBlock = {
                emptyView(hint = resources.getString(R.string.now_no_data))
            }).lparams(matchParent, matchParent)

            // 绑数据
            viewModel.normalList.bindOptional(context){
                if (it?.isEmpty() == true || it?.size == 0){
                    statusLayout.showEmpty()
                }else {
                    if (swipeToLayout.getIsRefreshing()){
                        swipeToLayout.finishRefresh()
                    }
                    statusLayout.showSuccess()
                    normalAdapter.setNewData(it)
                }
            }
        }
    }
}


/**
 * 停用
 */
class DisableUI(
    private val activity: AppCompatActivity,
    private val viewModel: CustomerDataViewModel
) {
    private lateinit var swipeToLayout: SwipeToLoadLayout
    private lateinit var statusLayout: StatusLayout

    private val disableAdapter: BaseAdapter<UserInfoData, CustomerDataViewHolder> = BaseAdapter { _, _ -> CustomerDataViewHolder(activity, viewModel,"4") }

    init {
        viewModel.queryUserInfo(type = "4")
    }

    val root = _FrameLayout(activity).apply {
        verticalLayout {
            linearLayout {
                gravity = Gravity.CENTER_VERTICAL
                textView {
                    textResource = R.string.user_info_type
                    textSizeAuto = 24
                    textColorInt = R.color.hint_text_color
                }.lparams(wrapContent, wrapContent) {
                    marginStart = autoSize(32)
                }

                textView {
                    textResource = R.string.user_info_name
                    textColorInt = R.color.hint_text_color
                    textSizeAuto = 24
                }.lparams(wrapContent, wrapContent){
                    marginStart = autoSize(95)
                }

                textView {
                    textResource = R.string.user_info_phone
                    textColorInt = R.color.hint_text_color
                    textSizeAuto = 24
                }.lparams(wrapContent, wrapContent){
                    marginStart = autoSize(215)
                }

                textView {
                    textResource = R.string.status
                    textColorInt = R.color.hint_text_color
                    textSizeAuto = 24
                }.lparams(wrapContent, wrapContent){
                    marginStart = autoSize(80)
                }

            }.lparams(matchParent, autoSize(68))

            view {
                background = resources.getDrawable(R.color.line_hint_color)
            }.lparams(matchParent, autoSize(1))

            statusLayout(contentBlock = {
                statusLayout = this
                setRetryAction {
                    viewModel.queryUserInfo(type = "4")
                }
//                bindTaskStatus(viewModule.status)
                swipeToLoadLayout {
                    swipeToLayout = this
                    setEnableRefresh(true)
                    setEnableLoadMore(false)
                    setOnRefreshListener {
                        viewModel.queryUserInfo(type = "4")
                    }
                    setEnableScrollContentWhenLoaded(false)
                    setEnableLoadMoreWhenContentNotFull(false)
                    recyclerView {
                        background = resources.getDrawable(R.color.white)
                        adapter = disableAdapter
                    }.lparams(matchParent, matchParent)
                }
            }, emptyBlock = {
                emptyView(hint = resources.getString(R.string.now_no_data))
            }).lparams(matchParent, matchParent)

            // 绑数据
            viewModel.disableList.bindOptional(context){
                if (it?.isEmpty() == true || it?.size == 0){
                    statusLayout.showEmpty()
                }else {
                    if (swipeToLayout.getIsRefreshing()){
                        swipeToLayout.finishRefresh()
                    }
                    statusLayout.showSuccess()
                    disableAdapter.setNewData(it)
                }
            }
        }
    }
}