package play.utils

import com.hikvision.formatconversion.HikFormatConversion
import com.hikvision.formatconversion.TransProgress
import com.hikvision.hatomplayer.util.FileUtil

class FileTransClient {
    companion object {
        const val OPTION_SUCCESS = 0
        const val OPTION_FAIL = -1
    }

    private val transProgress by lazy {
        TransProgress()
    }

    @Volatile
    private var formatHandle = -1L

    fun create(): Int {
        val createHandle = HikFormatConversion.createHandle()
        if (createHandle <= -1L) {
            return OPTION_FAIL
        }
        formatHandle = createHandle
        return OPTION_SUCCESS
    }

    fun start(filePath: String, targetPath: String): Int {
        if (formatHandle == -1L) {
            return OPTION_FAIL
        }
        if (!HikFormatConversion.startTransFile(
                formatHandle,
                filePath,
                targetPath,
                FileUtil.getExternalAppFilesPath() + "/font/songti.ttf"
            )
        ) {
            return OPTION_FAIL
        }
        return OPTION_SUCCESS
    }

    fun getProgress(): Float {
        if (formatHandle == -1L) {
            return -1f
        }
        if (!HikFormatConversion.getProgress(formatHandle, transProgress)) {
            return -1f
        }
        return transProgress.percent
    }

    fun stop(): Int {
        if (formatHandle == -1L) {
            return OPTION_SUCCESS
        }
        HikFormatConversion.stop(formatHandle)
        return OPTION_SUCCESS
    }

    fun destroy(): Int {
        if (formatHandle == -1L) {
            return OPTION_SUCCESS
        }
        HikFormatConversion.stop(formatHandle)
        formatHandle = -1L
        return OPTION_SUCCESS
    }
}
