package es.verdnatura.presentation.view.feature.pasillero.fragment

import android.annotation.SuppressLint
import android.content.Context
import es.verdnatura.MobileApplication
import es.verdnatura.R
import es.verdnatura.domain.ConstAndValues.RESERVATIONMODE
import es.verdnatura.domain.ConstAndValues.SECTORISONRESERVATIONMODE
import es.verdnatura.domain.SalixCallback
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.view.feature.pasillero.model.CodeWorkerAction
import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO
import es.verdnatura.presentation.view.feature.pasillero.model.WorkerActionSalix
import kotlinx.coroutines.runBlocking
import retrofit2.Response

class PasilleroViewModel(context: Context) : BaseViewModel(context) {
    @SuppressLint("StaticFieldLeak")
    private val contextApp = context
    private val _pasillerositem by lazy { ArrayList<PasillerosItemVO>() }
    val pasillerositem: List<PasillerosItemVO>
        get() = _pasillerositem
    val userId = (contextApp as MobileApplication).userId
    private val isOnReservationMode =
        (contextApp as MobileApplication).dataStoreApp.readDataStoreKey<Boolean>(RESERVATIONMODE)
    private val isOnReservationModeSector =
        (contextApp as MobileApplication).dataStoreApp.readDataStoreKey<Boolean?>(
            SECTORISONRESERVATIONMODE
        )
    private var existSectorOnReservationMode: Boolean = false

    init {
        runBlocking {
            existSectorOnReservationMode =
                (contextApp as MobileApplication).dataStoreApp.isPreferenceExists((SECTORISONRESERVATIONMODE))
        }
    }

    fun inititializeDefaultData() {
        workerActivityAdd(CodeWorkerAction.STORAGE)
        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_item, R.string.titleItemConsult, R.string.titleItemConsultDescrip
            )
        )

        /*

                _pasillerositem.add(
                    PasillerosItemVO(
                        6,
                        R.drawable.ic_local_parking_orange_24dp,
                       R.string.Parking),
                        R.string.Parking,
                       R.string.titleParkingDescrip)
                    )
                )
        */

        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_visibility_black_24dp,
                R.string.titleItemSearch,
                R.string.titleItemSearchDescrip
            )
        )

        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_dashboard_black_24dp,
                R.string.titleUbicator,
                R.string.titleUbicatorDescrip

            )
        )

        //tarea 7855
        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_scan_prepared,
                R.string.scanPreparedExpedition,
                R.string.scanPreparedExpedition
            )
        )


        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_spa_black_24dp,
                R.string.titleInventory,
                R.string.titleInventoryDescrip
            )
        )
        //Tarea 5891
        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_inventory_parking,
                R.string.titleInventoryParking,
                R.string.titleInventoryParkingDescrip
            )
        )

        /*    _pasillerositem.add(
                PasillerosItemVO(
                    100,
                    R.drawable.ic_category,
                   R.string.titleChangeCategory),
                    R.string.titleChangeCategory,
                   
                        R.string.titleChangeCategory
                    )
                )
            )*/

        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.worker_mistake,
                R.string.titleWorkerMistake,
                R.string.titleWorkerMistakeDescrip
            )
        )


        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_historical_menu, R.string.historicals, R.string.historicalsDescrip
            )
        )

        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.day_of_sale, R.string.titleDayOfSale, R.string.titleDayDescrip
            )
        )
        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_shopping_self, R.string.selfConsumption, R.string.titleSelfConsumption
            )
        )

    }

    fun inititializeDefaultPallet() {
        workerActivityAdd(CodeWorkerAction.PALLETIZING)
        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.pallet_shipping_selected,
                R.string.titlePalletizar,
                R.string.titlePalletDescrip
            )
        )

        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_pallet_scan_expedition,
                R.string.titleScanPalletizar,
                R.string.titleScanPalletDescrip
            )
        )

        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.lift, R.string.scanPlatform, R.string.titleScanPlatform
            )
        )

        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_expedition,
                R.string.titleExpeditionState,
                R.string.titleExpeditionDescrip
            )

        )

        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_cmr, R.string.titleCMRState, R.string.titleCMRDescrip
            )
        )
        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.barcode_scan, R.string.titleExpeditionScan, R.string.titleExpScanDescrip
            )
        )


        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.buffer_outline,
                R.string.titleBufferManegement,
                R.string.titleBufferMangDescrip
            )
        )

        /* _pasillerositem.add(
             PasillerosItemVO(
                 33,
                 R.drawable.qrcode_scan,
                R.string.titleQrReader),
                 R.string.titleQrReader,
                R.string.titleQrDescrip)
             )
         )*/

    }

    fun inititializeDefaultBuffer() {

        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.buffer_empty, R.string.titleBufferEmpty, R.string.titleBufferEmptyDescrip
            )
        )


        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.buffer_fill, R.string.titleBufferFill, R.string.titleBufferFillDescrip
            )
        )


        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.buffer_accumulation,
                R.string.titleBufferAccumulate,
                R.string.titleBufferAccDescrip
            )
        )

        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.buffer_off, R.string.titleBufferOff, R.string.titleBufferOffDescrip
            )
        )

        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.buffer_flejado,
                R.string.titleBufferStrapping,
                R.string.titleBufferStrapDescrip
            )
        )


        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.buffer_load, R.string.titleBufferload, R.string.titleBufferLoadDescrip
            )
        )
    }

    fun inititializeDefaultDataInit(pasillero: PasillerosItemVO? = null) {
        val userId = (contextApp as MobileApplication).userId
        if (pasillero != null) {
            _pasillerositem.add(pasillero)
        } else {
            _pasillerositem.add(
                PasillerosItemVO(
                    R.drawable.ic_previous_menu,
                    R.string.titlePrevious,
                    R.string.titlePreviousDescription
                )
            )

            _pasillerositem.add(
                PasillerosItemVO(
                    R.drawable.ic_corridor_ui,
                    R.string.titleCorridors,
                    R.string.titleCorridorsDescrip
                )
            )
            //sacador
            if (isOnReservationModeSector == true || (!existSectorOnReservationMode && isOnReservationMode)) {
                _pasillerositem.add(
                    PasillerosItemVO(
                        R.drawable.ic_picker_ui, R.string.sacador_test, R.string.sacador_test
                    )
                )
            } else {
                _pasillerositem.add(
                    PasillerosItemVO(
                        R.drawable.ic_picker_ui, R.string.titlePickers, R.string.titlePickersDescrip

                    )
                )

            }
            if (userId == 19591) {
                _pasillerositem.add(
                    PasillerosItemVO(
                        R.drawable.ic_picker_helper,
                        R.string.pickerHelper,
                        R.string.pickerHelperDescrip
                    )
                )
            }

            _pasillerositem.add(
                PasillerosItemVO(
                    R.drawable.ic_controller_ui,
                    R.string.titleControllers,
                    R.string.titleControlDescrip
                )
            )

            _pasillerositem.add(
                PasillerosItemVO(
                    R.drawable.ic_pallet_ui,
                    R.string.titlePalletizers,
                    R.string.titlePalletizDescrip
                )
            )


            _pasillerositem.add(
                PasillerosItemVO(
                    R.drawable.ic_claim_ui, R.string.titleClaims, R.string.titleClaimsDescrip
                )
            )

            //Tarea 4678
            _pasillerositem.add(
                PasillerosItemVO(
                    R.drawable.ic_delivery_truck, R.string.titleDelivery, R.string.titleDelivery
                )
            )

            _pasillerositem.add(
                PasillerosItemVO(
                    R.drawable.ic_troncales,
                    R.string.titleDeliveryTruck,
                    R.string.titleDeliveryTruck
                )
            )

            //#tarea4021

            _pasillerositem.add(
                PasillerosItemVO(
                    R.drawable.ic_packaging_entries,
                    R.string.titlePackaging,
                    R.string.titlePackagingDescrip
                )
            )

            _pasillerositem.add(
                PasillerosItemVO(
                    R.drawable.photo_camera, R.string.titlePhotos, R.string.titlePhotos
                )
            )
            _pasillerositem.add(
                PasillerosItemVO(
                    R.drawable.ic_other, R.string.titleOther, R.string.titleOther
                )
            )

            val workingInTest = false
            if (workingInTest) {
                _pasillerositem.add(
                    PasillerosItemVO(
                        R.drawable.ic_star_ko, R.string.testing, R.string.testing
                    )
                )
            }
        }
    }

    fun inititializeDefaultClaim() {
        workerActivityAdd(CodeWorkerAction.CLAIM)
        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_claims, R.string.titleClaimUbication, R.string.titleClaimDescrip
            )
        )


        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_reubication, R.string.titleReUbicator, R.string.titleReubicatorDescrip
            )
        )

    }

    fun inititializeDefaultDelivery() {
        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_delivery_truck,
                R.string.titleDeliveryRoute,
                R.string.titleDeliveryRoute
            )
        )

    }

    fun inititializeDefaultDeliveryTruck() {

        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_troncales, R.string.titleDeliveryTruck, R.string.titleDeliveryTruck
            )
        )

    }

    fun inititializeDefaultPrevia() {
        workerActivityAdd(CodeWorkerAction.PREVIOUS)

        if (isOnReservationModeSector == true || (!existSectorOnReservationMode && isOnReservationMode)) {
            _pasillerositem.add(
                PasillerosItemVO(
                    R.drawable.ic_previous_presacador,
                    R.string.titlePrePickerTest,
                    R.string.titlePickerDescrip
                )
            )
        } else {
            _pasillerositem.add(
                PasillerosItemVO(
                    R.drawable.ic_previous_presacador,
                    R.string.titlePrePicker,
                    R.string.titlePickerDescrip
                )
            )
        }


        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_previous_precontrol, R.string.titlePreControl,

                R.string.titlePreControlDescrip
            )
        )

        /* Para preControl de reservas, faltan cosas no se puede todavía.
        if (userId == 19591) {
               _pasillerositem.add(
                   PasillerosItemVO(
                       R.drawable.ic_previous_precontrol, R.string.preControlNew,

                       R.string.preControlNew
                   )
               )
           }*/

        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_ticket, R.string.titleShowTicket, R.string.titleShowTicketDescrip
            )
        )

    }

    fun inititializeDefaultChecker() {
        workerActivityAdd(CodeWorkerAction.ON_CHECKING)
        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_verified_user_black_24dp_selected,
                R.string.titleControllersAction,
                R.string.titleCheckerDescrip
            )
        )


        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_review_boxpicking,
                R.string.reviewBoxPicking,
                R.string.reviewBoxPicking
            )
        )

        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_advance_ticket, R.string.ticketAdvance, R.string.ticketAdvanceDescrip
            )
        )

        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_delete_expedition,
                R.string.deleteExpedition,
                R.string.deleleExpeditionDescrip
            )
        )


        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_packing_mistake,
                R.string.titlePackingMistake,
                R.string.titlePackingMistakeDescrip
            )
        )

    }

    fun inititializeDefaultOther() {

        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_restaurant,
                R.string.titleRestaurant,
                R.string.resturantDescrip
            )
        )

        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_packaging, R.string.titlePackingHolland, R.string.titleUbicatorDescrip
            )
        )

    }

    fun inititializeDefaultHistorical() {
        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.revision_icon,
                R.string.titleShelvingHistorical,
                R.string.titleShelvingHistDescrip
            )
        )
        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_history_orange,
                R.string.titleLogShelving,
                R.string.titleLogShelvingDescrip
            )
        )

        //Tarea 5186
        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.ic_parking_previa,
                R.string.titleParkingTicketPrevia,
                R.string.titleParkingTicketPrevia
            )
        )

        _pasillerositem.add(
            PasillerosItemVO(
                R.drawable.car_info,
                R.string.titleHistoricalVehicle,
                R.string.titleHistVehicleDescrip
            )
        )

    }

    fun workerActivityAdd(
        codeActivity: CodeWorkerAction
    ) {
        //Tarea 6078
        salix.workerActivityAdd(WorkerActionSalix(codeActivity.toString()))

            .enqueue(object : SalixCallback<Any>(contextApp) {
                override fun onSuccess(response: Response<Any>) {
                }

            })

    }

}
