package cn.sagacloud.server.algorithm.backstage

import cn.sagacloud.server.algorithm.backstage.state.*
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.scheduling.annotation.EnableScheduling
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

/**
 *
 *
 * @author jxing
 */
@Component
@EnableScheduling
class BackMainThread {
    companion object{
        private val logger = LoggerFactory.getLogger(BackMainThread::class.java)
        val states = ArrayList<ModelState>()
    }

    @Autowired
    lateinit var state0 :UploadState_0

    @Autowired
    lateinit var state1 :UploadedState_1

    @Autowired
    lateinit var state10 :DataCheckState_10

    @Autowired
    lateinit var state11 :CheckErrorState_11

    @Autowired
    lateinit var state2 :CheckPassedState_2

    @Autowired
    lateinit var state20 :DataExportState_20

    @Autowired
    lateinit var state21 :ExportErrorState_21

    @Autowired
    lateinit var state3 :ExportPassedState_3

    @Autowired
    lateinit var state31 :SyncDataErrorState_31

    @Autowired
    lateinit var state4 :FinishState_4

    /**
     * 定时任务, 处理它们的状态, 当前未完成时, 不会执行下一个scheduled
     */
    //@Scheduled(cron = "0 0/1 * * * ?")
    @Scheduled(fixedRate = 1000 * 60 * 3, initialDelay = 1000)
    fun run() {
        if(states.isEmpty()) {
//            states.addAll(arrayListOf(state31, state3))
//            states.addAll(arrayListOf(state1, state10, state11, state2))
            states.addAll(arrayListOf(state0, state1, state10, state11, state2, state20, state21, state31, state3, state4))
        }
        states.forEach { modelState ->
            modelState.prepareModel()
            modelState.handle()
        }
    }
}
