package es.verdnatura.presentation.view.feature.collection.mapper

import android.content.Context
import es.verdnatura.R
import es.verdnatura.presentation.common.convertToDateString
import es.verdnatura.presentation.view.feature.sacador.model.CollectionVO
import java.text.SimpleDateFormat
import java.util.*

fun CollectionVO.map(context: Context): CollectionVO {
    try {


        this.tickets.forEach { ticket ->
            ticket.sales.forEach {
                it.ticketFk = ticket.ticketFk
                it.level = ticket.level
                it.rgb = it.rgb
                it.salePersonFk = ticket.salesPersonFk
                it.agencyName = ticket.agencyName
                it.isNew = it.isAdded == "1"

                // sergio: OBLIGAR A ACTUALIZAR VERSIÓN PARA SUBIR DESPUÉS FICHEROS DEL BACK collection_getTickets al mismo tiempo")

                //it.quantity = it.quantity.substring(0, it.quantity.indexOf("."))

                //Solución Tarea #3420
                //Log.d("VERDNATURA::","quantity"+it.quantity)
                //Log.d("VERDNATURA::","quantity original"+it.originalQuantity)

                it.quantity = if (it.quantity.indexOf(".") > 0) {
                    it.quantity.substring(0, it.quantity.indexOf("."))
                } else {
                    it.quantity
                }

                it.originalQuantity = (if (it.originalQuantity.indexOf(".") > 0) {
                    it.originalQuantity.substring(0, it.originalQuantity.indexOf("."))
                } else it.originalQuantity)

                if (it.originalQuantity.equals("-1")) it.originalQuantity = it.quantity

                /* it.originalQuantity =
                     if (it.originalQuantity != "-1") it.originalQuantity.substring(
                         0,
                         it.originalQuantity.indexOf(".")
                     ) else it.quantity*/


                it.pickedQuantity =
                    if (it.isPrepared == "1" || it.isPreviousPrepared == "1" || it.isControlled == "1") it.quantity else it.pickedQuantity
                if (!it.placements.isNullOrEmpty()) {
                    it.placements.forEachIndexed { index, placement ->
                        if (index == 0) {
                            try {
                                it.saleOrder = placement.saleOrder.toInt()
                            } catch (e: NumberFormatException) {
                                it.saleOrder = 9999
                            }
                        }
                        placement.created =
                            getCalendarFromDate(placement.created,context).convertToDateString()
                        placement.visible =
                            "(" + (if (placement.visible.toInt() > 0) placement.visible else "0") + ")"
                    }
                } else {
                    it.placements = listOf()
                }



            }
        }
    } catch (e: Exception) {
        this.isError = true
        if (this.tickets.isNullOrEmpty()) {
            this.errorMessage = context.getString(R.string.collectionNoTicketsError)
        } else {

            this.errorMessage =
                context.getString(R.string.collectionErrorBuilding) + e.message.toString()
        }

    }
    return this
}

private fun getCalendarFromDate(date: String,context:Context): Calendar {
    val sdf = SimpleDateFormat(context.getString(R.string.dateFormat))
    val cal = Calendar.getInstance()
    cal.time = sdf.parse(date)!!
    return cal
}