import { type Ref, computed, defineComponent, inject, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { useRoute, useRouter } from 'vue-router';

import { useVuelidate } from '@vuelidate/core';

import UtenteAppService from '@/entities/utente-app/utente-app.service';
import { useAlertService } from '@/shared/alert/alert.service';
import { useValidation } from '@/shared/composables';
import { Conferma, type IConferma } from '@/shared/model/conferma.model';
import { TipoConferma } from '@/shared/model/enumerations/tipo-conferma.model';
import { type IUtenteApp } from '@/shared/model/utente-app.model';

import ConfermaService from './conferma.service';

export default defineComponent({
  name: 'ConfermaUpdate',
  setup() {
    const confermaService = inject('confermaService', () => new ConfermaService());
    const alertService = inject('alertService', () => useAlertService(), true);

    const conferma: Ref<IConferma> = ref(new Conferma());

    const utenteAppService = inject('utenteAppService', () => new UtenteAppService());

    const utenteApps: Ref<IUtenteApp[]> = ref([]);
    const tipoConfermaValues: Ref<string[]> = ref(Object.keys(TipoConferma));
    const isSaving = ref(false);
    const currentLanguage = inject('currentLanguage', () => computed(() => navigator.language ?? 'it'), true);

    const route = useRoute();
    const router = useRouter();

    const previousState = () => router.go(-1);

    const retrieveConferma = async confermaId => {
      try {
        const res = await confermaService().find(confermaId);
        conferma.value = res;
      } catch (error) {
        alertService.showHttpError(error.response);
      }
    };

    if (route.params?.confermaId) {
      retrieveConferma(route.params.confermaId);
    }

    const initRelationships = () => {
      utenteAppService()
        .retrieve()
        .then(res => {
          utenteApps.value = res.data;
        });
    };

    initRelationships();

    const { t: t$ } = useI18n();
    const validations = useValidation();
    const validationRules = {
      motivoConferma: {},
      tipoConferma: {},
      confermataDa: {},
      prenotazione: {},
    };
    const v$ = useVuelidate(validationRules, conferma as any);
    v$.value.$validate();

    return {
      confermaService,
      alertService,
      conferma,
      previousState,
      tipoConfermaValues,
      isSaving,
      currentLanguage,
      utenteApps,
      v$,
      t$,
    };
  },
  created(): void {},
  methods: {
    save(): void {
      this.isSaving = true;
      if (this.conferma.id) {
        this.confermaService()
          .update(this.conferma)
          .then(param => {
            this.isSaving = false;
            this.previousState();
            this.alertService.showInfo(this.t$('smartbookingApp.conferma.updated', { param: param.id }));
          })
          .catch(error => {
            this.isSaving = false;
            this.alertService.showHttpError(error.response);
          });
      } else {
        this.confermaService()
          .create(this.conferma)
          .then(param => {
            this.isSaving = false;
            this.previousState();
            this.alertService.showSuccess(this.t$('smartbookingApp.conferma.created', { param: param.id }).toString());
          })
          .catch(error => {
            this.isSaving = false;
            this.alertService.showHttpError(error.response);
          });
      }
    },
  },
});
