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.CollectionTicket
import es.verdnatura.presentation.view.feature.sacador.model.CollectionVO
import es.verdnatura.presentation.view.feature.sacador.model.PlacementSupplyVO
import java.text.SimpleDateFormat
import java.util.Calendar

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

        this.tickets.forEach { ticket ->
            // if (ticket.sales.size>0){
            ticket.sales.forEach {
                it.ticketFk = ticket.ticketFk
                it.level = ticket.level
                it.rgb = ticket.rgb
                it.salePersonFk = ticket.salesPersonFk
                it.agencyName = ticket.agencyName
                it.isNew = it.isAdded == "1"
                it.code = it.cel3
                it.quantity = it.quantity!!.toInt()
                it.originalQuantity = it.originalQuantity!!.toInt()
                if (it.originalQuantity == -1) it.originalQuantity = 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
}

fun CollectionTicket.map(context: Context): CollectionTicket {
    try {
        this.tickets.forEach { ticket ->
            ticket.sales.forEach {
                it.level = ticket.level ?: ""
                it.rgb = ticket.rgb ?: ""
            }
        }

    } 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) + context.getString(R.string.reviewSector)
        }

    }
    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
}

// para cuando se pase a Salix el modelo
//Tarea 5134
fun PlacementSupplyVO.proposal(context: Context): PlacementSupplyVO {
    try {
    } catch (ex: Exception) {
    }
    return this
}