package llc.arma.ble.data

import android.R.attr.src
import android.app.Application
import android.icu.text.SimpleDateFormat
import android.os.Environment
import android.os.FileUtils
import llc.arma.ble.R
import llc.arma.ble.domain.model.Ble
import llc.arma.ble.domain.repository.XlsxRepository
import org.apache.poi.ss.usermodel.WorkbookFactory
import org.apache.poi.util.IOUtils
import org.apache.poi.xssf.usermodel.XSSFSheet
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.Date
import java.util.UUID
import javax.inject.Inject


class XlsxRepositoryImpl @Inject constructor(
    private val application: Application
) : XlsxRepository {

    override fun exportToXls(data: List<Ble.Accelerometer.HistoryPoint>): File {

        val formatter = SimpleDateFormat("dd.MM.yyyy HH:mm")

        val mailFile = File(application.filesDir, "${UUID.randomUUID()}.xlsx")
        mailFile.createNewFile()

        when(data.firstOrNull()){
            is Ble.Accelerometer.HistoryPoint.Vibration -> {
                IOUtils.copy(application.resources.openRawResource(R.raw.measure_single_axis), FileOutputStream(mailFile))
            }
            else -> {
                IOUtils.copy(application.resources.openRawResource(R.raw.measure_multiple_axis), FileOutputStream(mailFile))
            }
        }

        val fileIn = FileInputStream(mailFile)
        val workbook = WorkbookFactory.create(fileIn)
        val worksheet = workbook.getSheetAt(0) as XSSFSheet

        data.withIndex().forEach {

            val row = worksheet.createRow(it.index + 1)
            val dateX = row.createCell(0)
            val dateY = row.createCell(2)
            val dateZ = row.createCell(4)
            val x = row.createCell(1)
            val y = row.createCell(3)
            val z = row.createCell(5)

            when(val value = it.value){
                is Ble.Accelerometer.HistoryPoint.Angle -> {
                    dateX.setCellValue(formatter.format(Date(value.date)))
                    dateY.setCellValue(formatter.format(Date(value.date)))
                    dateZ.setCellValue(formatter.format(Date(value.date)))
                    x.setCellValue(value.x.toDouble())
                    y.setCellValue(value.y.toDouble())
                    z.setCellValue(value.z.toDouble())
                }
                is Ble.Accelerometer.HistoryPoint.Vibration -> {
                    dateX.setCellValue(formatter.format(Date(value.date)))
                    x.setCellValue(value.value.toDouble())
                }

                is Ble.Accelerometer.HistoryPoint.Accelerate -> {
                    dateX.setCellValue(formatter.format(Date(value.date)))
                    dateY.setCellValue(formatter.format(Date(value.date)))
                    dateZ.setCellValue(formatter.format(Date(value.date)))
                    x.setCellValue(value.x.toDouble())
                    y.setCellValue(value.y.toDouble())
                    z.setCellValue(value.z.toDouble())
                }

            }
        }

        fileIn.close()
        val saveFos = FileOutputStream(mailFile)
        workbook.write(saveFos)
        saveFos.close()

        val sharedFile = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "${UUID.randomUUID()}.xlsx")
        sharedFile.createNewFile()

        val sharedSaveFos = FileOutputStream(sharedFile)
        workbook.write(sharedSaveFos)
        sharedSaveFos.close()

        workbook.close()

        return mailFile
    }

}