package com.grkj.iscs.view.activity

import android.view.Gravity
import com.grkj.iscs.R
import com.grkj.iscs.view.base.BaseMvpActivity
import com.grkj.iscs.databinding.ActivityCreateTicketBinding
import com.grkj.iscs.view.iview.ICreateTicketView
import com.grkj.iscs.model.vo.ticket.CreateTicketReqVO
import com.grkj.iscs.model.vo.sop.SopPageRespVO
import com.grkj.iscs.view.presenter.CreateTicketPresenter
import com.grkj.iscs.util.ToastUtils
import com.grkj.iscs.view.widget.SelectableInput
import com.manu.mdatepicker.MDatePicker
import java.text.SimpleDateFormat
import java.util.Date
import java.util.stream.Collectors

class CreateTicketActivity : BaseMvpActivity<ICreateTicketView, CreateTicketPresenter, ActivityCreateTicketBinding>(),
    ICreateTicketView {

    private var mSelectedSopIdx: Int? = null

    override val viewBinding: ActivityCreateTicketBinding
        get() = ActivityCreateTicketBinding.inflate(layoutInflater)

    override fun initView() {
        presenter?.initData()

        mBinding?.sAutocode?.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                presenter?.getAutoCode { mBinding?.siTicketNumber?.setText(it) }
            } else {
                mBinding?.siTicketNumber?.setText(null)
            }
        }

        mBinding?.tvStartTime?.setOnClickListener {
            MDatePicker.create(this)
                .setCanceledTouchOutside(true)
                .setGravity(Gravity.CENTER)
                .setSupportTime(true)
                .setTwelveHour(false)
                .setOnDateResultListener {
                    mBinding?.tvStartTime?.text = SimpleDateFormat("yyyy-MM-dd HH:mm").format(Date(it))
                }
                .build().show()
        }

        mBinding?.tvEndTime?.setOnClickListener {
            MDatePicker.create(this)
                .setCanceledTouchOutside(true)
                .setGravity(Gravity.CENTER)
                .setSupportTime(true)
                .setTwelveHour(false)
                .setOnDateResultListener {
                    mBinding?.tvEndTime?.text = SimpleDateFormat("yyyy-MM-dd HH:mm").format(Date(it))
                }
                .build().show()
        }

        mBinding?.confirm?.setOnClickListener {
            if (mSelectedSopIdx == null) {
                ToastUtils.tip(resources.getString(R.string.sop_is_null))
                return@setOnClickListener
            }
            val ticketVO = CreateTicketReqVO(
                mBinding?.siTicketNumber?.getText(),
                mBinding?.siTicketName?.getText(),
                presenter?.mSopList?.get(mSelectedSopIdx!!)?.workshopId,
                presenter?.mSopList?.get(mSelectedSopIdx!!)?.workareaId,
                presenter?.mSopList?.get(mSelectedSopIdx!!)?.sopId,
                presenter?.mTicketTypeList?.get(mBinding?.siTicketType?.mSelectIdx!!)?.dictType,
                mBinding?.siContent?.getText(),
                mBinding?.tvStartTime?.text.toString(),
                mBinding?.tvEndTime?.text.toString(),
                mBinding?.siSegregationPoint?.getText().toString().replace("[", "").replace("]", ""),
                // TODO 待补充
                null
            )
            presenter?.createTicket(ticketVO)
        }

        mBinding?.exit?.setOnClickListener { finish() }
    }

    override fun showSopList(sopList: MutableList<SopPageRespVO.Record>) {
        mBinding?.siSop?.setOptionList(sopList.stream().map { it.sopName }.collect(Collectors.toList()))
        mBinding?.siSop?.setOnSpinnerSelectListener(object : SelectableInput.OnSpinnerSelectListener {
            override fun onSelect(str: String?, index: Int) {
                presenter?.getSopInfo(sopList[index].sopId) {
                    mBinding?.siSegregationPoint?.setText(it.toString())
                }
                mSelectedSopIdx = index
                val sop = sopList[index]
                mBinding?.siWorkshop?.setText(sop.workshopName)
                mBinding?.siLine?.setText(sop.workareaName)
                mBinding?.siTicketName?.setText("${sop.sopName}-${sop.workshopName}-${sop.workareaName}")
                mBinding?.siTicketType?.setText(presenter?.getTicketTypeName(sop.sopType))
            }
        })
    }

    override fun updateLockerList(lockerList: List<String>?) {
        lockerList?.let {
            mBinding?.siPersonLock?.setOptionList(it as MutableList<String>)
        }
    }

    override fun initPresenter(): CreateTicketPresenter {
        return CreateTicketPresenter()
    }
}