package com.duolebo.blyrobot.tools

import android.util.Log
import com.duolebo.blyrobot.data.Task
import java.util.*
import kotlin.collections.ArrayList

class TaskManager {

    companion object {
        val instance = TaskManager()
        val TAG = "RobotTaskManager"
        val RUN_TASK_PERIOD: Long = 6 * 60 * 60 * 1000
    }

    val tasks = ArrayList<Task>()
    var isRunning = false

    private val overTasks = ArrayList<Task>()
    private var currentTask: Task? = null
    private var scheduleTime: Timer? = null

    fun add(task: Task) {
        for (existTask in tasks) {
            Log.i(TAG, "exist task app: ${existTask.apkInfo.packageName}")
        }
        task.taskListener = object : Task.OnTaskListener {
            override fun onComplete(result: Boolean) {
                Log.i(TAG, "task ${task.apkInfo.name} complete with result: $result")
                tasks.remove(task)
                overTasks.add(task)

                if (tasks.size == 0) {
                    isRunning = false
                }
            }
        }
        if (isExist(task))
            remove(task)
        this.tasks.add(task)
    }

    fun remove(task: Task) {
        val item = tasks.find {
            it.apkInfo.packageName == task.apkInfo.packageName
        }

        item?.run {
            Log.i(TAG, "remove task ${this.apkInfo.packageName}")
            tasks.remove(this)
            this.release()
        }
    }

    fun isExist(task: Task): Boolean {
        val item = tasks.find {
            it.apkInfo.packageName == task.apkInfo.packageName
        }

        return item != null
    }

    fun start() {
        Log.i(TAG, "start...")
        if (this.isRunning) {
            Log.i(TAG, "is running")
            return
        }

        if (this.tasks.size == 0) {
            Log.i(TAG, "no tasks")
            return
        }

        this.isRunning = true
        scheduleTime?.run {
            cancel()
        }
        scheduleTime = Timer()
        scheduleTime!!.schedule(object : TimerTask() {
            override fun run() {
                while (isRunning) {

                    if (currentTask != null && currentTask!!.isRunning()) {
                        Log.i(TAG, "task ${currentTask!!.apkInfo.packageName} is running")
                    }
                    else {
                        if (tasks.size == 0) {
                            for (task in overTasks) {
                                task.reset()
                                tasks.add(task)
                            }
                        }

                        if (tasks.size > 0) {
                            currentTask = tasks[0]
                            currentTask!!.start()
                            Log.i(TAG, "task ${currentTask!!.apkInfo.packageName} start")
                        }
                    }

                    Thread.sleep(3 * 1000)
                }
            }
        }, 0, RUN_TASK_PERIOD)
    }

    fun stop() {
        this.isRunning = false
        scheduleTime?.run {
            cancel()
        }
        this.tasks.forEach {
            it.exit()
        }

        this.tasks.clear()
        this.overTasks.clear()
    }
}