import { defineStore, acceptHMRUpdate } from 'pinia';
import { socket } from 'boot/socketio';
import { useSocketStore } from 'stores/socket';
import type { Favorite } from 'components/models';

export const useFavoriteStore = defineStore('favoriteStore', {
  state: () => {
    return {
      favorites: [] as Favorite[],
      categories: [] as string[],
    };
  },
  getters: {},
  actions: {
    favoriteControl({ command, favorite }: { command: string; favorite?: Favorite }) {
      const socketStore = useSocketStore();
      const debugLog = socketStore.debugLog;
      let fnctRtn: { sts: string; msg?: string | undefined } = { sts: '', msg: '' };
      switch (command) {
        case 'set':
          if (!favorite) {
            console.log('Favorite not provided, favdata: %s', favorite);
            fnctRtn = { sts: 'error', msg: 'Favorite not provided.' };
            throw new Error('Favorite  not provided.');
          }
          socket.emit('favorite_control', { command: 'set', favorite: favorite }, (response) => {
            if (response.command_status == 'ERROR') {
              throw new Error(response.message);
            } else {
              if (!response.data?.favorites) {
                fnctRtn = { sts: 'error', msg: 'No favorites found.' };
                throw new Error(response.message);
              } else {
                console.log('Favorite added, favdata: %s', favorite);
                fnctRtn = { sts: 'OK', msg: response.message?.toString() };
                this.favorites = response.data.favorites;
                response.data.favorites.forEach((fav) => {
                  if (!this.categories.includes(fav.category)) {
                    this.categories.push(fav.category);
                  }
                });
              }
              if (debugLog) {
                console.log('response from favorite_control_add: ', response);
              }
              return response.message;
            }
          });
          break;
        case 'delete':
          socket.emit('favorite_control', { command: 'delete', favorite: favorite }, (response) => {
            if (response.command_status == 'ERROR') {
              fnctRtn = { sts: 'error', msg: response.message };
              throw new Error(response.message);
            } else {
              if (!response.data?.favorites) {
                fnctRtn = { sts: 'error', msg: 'No favorites found.' };
                throw new Error(response.message);
              } else {
                fnctRtn = { sts: 'OK', msg: response.message?.toString() };
                this.favorites = response.data.favorites;
                response.data.favorites.forEach((fav) => {
                  if (!this.categories.includes(fav.category)) {
                    this.categories.push(fav.category);
                  }
                });
              }
              if (debugLog) {
                console.log('response from favorite_control_delete: ', response);
              }
              return response.message;
            }
          });
          break;
        case 'get':
          socket.emit('favorite_control', { command: 'get', favorite: favorite }, (response) => {
            if (response.command_status == 'ERROR') {
              fnctRtn = { sts: 'error', msg: response.message };
              throw new Error(response.message);
            } else {
              if (!response.data?.favorites) {
                fnctRtn = { sts: 'error', msg: 'No favorites found.' };
                throw new Error(response.message);
              } else {
                fnctRtn = { sts: 'OK', msg: response.message?.toString() };
                this.favorites = response.data.favorites;
                response.data.favorites.forEach((fav) => {
                  if(!this.categories.includes(fav.category)) {
                    this.categories.push(fav.category);
                  }
                })
              }
              if (debugLog) {
                console.log('response from favorite_control_get: ', response);
              }
              return response.message;
            }
          });
          break;
        default:
          fnctRtn = { sts: 'error', msg: 'Invalid command' + command };
          throw new Error('Invalid command' + command);
      }
      return fnctRtn;
    },
    initialize(): void {
      this.favoriteControl({ command: 'get' });
    },
    getCategories(): string[] {
      this.favoriteControl({ command: 'get' });
      const categories: string[] = [];
      this.favorites.forEach((favorite) => {
        if (!categories.includes(favorite.category)) {
          categories.push(favorite.category);
        }
      });
      return categories;
    },
  },
});

if (import.meta.hot) {
  import.meta.hot.accept(acceptHMRUpdate(useFavoriteStore, import.meta.hot));
}
