import React, { createContext, useContext, ReactNode } from 'react';
import { useMyIonStore } from './MyIonStore';

const MyIonFavoriteContext = createContext<MyContextProps | undefined>(undefined);

export const MyIonFavoriteProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
  const { myIonStoreRead, myIonStoreWrite, user_store } = useMyIonStore();

  async function myIonStoreAddFavorite(address_to_add: string) {
    console.log('myIonStoreAddFavorite', address_to_add);
    let current_fav = await JSON.parse(await myIonStoreRead('lesson_favorite_word'));
    if (JSON.stringify(current_fav) == '{}') current_fav = [];
    current_fav = [...current_fav, address_to_add];
    await myIonStoreWrite('lesson_favorite_word', JSON.stringify(current_fav));
  }

  async function myIonStoreRemoveFavorite(address_to_remove: string) {
    let current_fav = await JSON.parse(await myIonStoreRead('lesson_favorite_word'));
    current_fav = current_fav.filter((address: string) => address !== address_to_remove);
    await myIonStoreWrite('lesson_favorite_word', JSON.stringify(current_fav));
  }

  async function myIonStoreLoadFavorite() {
    let current_fav = await JSON.parse(await myIonStoreRead('lesson_favorite_word'));
    return current_fav ? current_fav : [];
  }

  async function myIonStoreFindInFavorite(string_to_search: string) {
    const current_fav = await myIonStoreLoadFavorite();
    return current_fav.includes(string_to_search);
  }

  //
  const KEY_FAV_VOCBULARY = 'lesson_favorite_vocabulary';
  async function myIonStoreAddFavoriteVocabulary(address_to_add: string) {
    console.log('myIonStoreAddFavorite', address_to_add);
    let current_fav = await JSON.parse(await myIonStoreRead(KEY_FAV_VOCBULARY));
    if (JSON.stringify(current_fav) == '{}') current_fav = [];
    current_fav = [...current_fav, address_to_add];
    await myIonStoreWrite(KEY_FAV_VOCBULARY, JSON.stringify(current_fav));
  }

  async function myIonStoreRemoveFavoriteVocabulary(address_to_remove: string) {
    let current_fav = await JSON.parse(await myIonStoreRead(KEY_FAV_VOCBULARY));
    current_fav = current_fav.filter((address: string) => address !== address_to_remove);
    await myIonStoreWrite(KEY_FAV_VOCBULARY, JSON.stringify(current_fav));
  }

  async function myIonStoreLoadFavoriteVocabulary(): Promise<string[]> {
    let result = await myIonStoreRead(KEY_FAV_VOCBULARY);

    if (result == '{}') return [];
    let current_fav = JSON.parse(result);
    return current_fav;
  }

  async function myIonStoreFindInFavoriteVocabulary(string_to_search: string): Promise<number> {
    const current_fav = await myIonStoreLoadFavoriteVocabulary();
    console.log({ current_fav });
    return current_fav.includes(string_to_search);
  }

  //
  const KEY_FAV_CONNECTIVES = 'lesson_favorite_connectives';

  async function myIonStoreAddFavoriteConnectives(address_to_add: string) {
    console.log('myIonStoreAddFavorite', address_to_add);
    let current_fav = await JSON.parse(await myIonStoreRead(KEY_FAV_CONNECTIVES));
    if (JSON.stringify(current_fav) == '{}') current_fav = [];
    current_fav = [...current_fav, address_to_add];
    await myIonStoreWrite(KEY_FAV_CONNECTIVES, JSON.stringify(current_fav));
  }

  async function myIonStoreRemoveFavoriteConnectives(address_to_remove: string) {
    let current_fav = await JSON.parse(await myIonStoreRead(KEY_FAV_CONNECTIVES));
    current_fav = current_fav.filter((address: string) => address !== address_to_remove);
    await myIonStoreWrite(KEY_FAV_CONNECTIVES, JSON.stringify(current_fav));
  }

  async function myIonStoreLoadFavoriteConnectives(): Promise<string[]> {
    let temp = await myIonStoreRead(KEY_FAV_CONNECTIVES);
    let current_fav = await JSON.parse(temp);
    return current_fav ? current_fav : [];
  }

  async function myIonStoreFindInFavoriteConnectives(string_to_search: string) {
    const current_fav = await myIonStoreLoadFavoriteConnectives();
    return current_fav.includes(string_to_search);
  }

  return (
    <MyIonFavoriteContext.Provider
      value={{
        myIonStoreAddFavorite,
        myIonStoreRemoveFavorite,
        myIonStoreLoadFavorite,
        myIonStoreFindInFavorite,
        //
        myIonStoreAddFavoriteVocabulary,
        myIonStoreRemoveFavoriteVocabulary,
        myIonStoreLoadFavoriteVocabulary,
        myIonStoreFindInFavoriteVocabulary,
        //
        myIonStoreAddFavoriteConnectives,
        myIonStoreRemoveFavoriteConnectives,
        myIonStoreLoadFavoriteConnectives,
        myIonStoreFindInFavoriteConnectives
      }}
    >
      {children}
    </MyIonFavoriteContext.Provider>
  );
};

export const useMyIonFavorite = (): MyContextProps => {
  const context = useContext(MyIonFavoriteContext);
  if (!context) {
    throw new Error('useMyContext must be used within a MyProvider');
  }
  return context;
};

interface MyContextProps {
  myIonStoreAddFavorite: (address_to_add: string) => Promise<void>;
  myIonStoreRemoveFavorite: (address_to_add: string) => Promise<void>;
  myIonStoreLoadFavorite: () => Promise<[]>;
  myIonStoreFindInFavorite: (string_to_search: string) => Promise<boolean>;
  //
  myIonStoreAddFavoriteVocabulary: (address_to_add: string) => Promise<void>;
  myIonStoreRemoveFavoriteVocabulary: (address_to_add: string) => Promise<void>;
  myIonStoreLoadFavoriteVocabulary: () => Promise<string[]>;
  myIonStoreFindInFavoriteVocabulary: (string_to_search: string) => Promise<boolean>;
  //
  myIonStoreAddFavoriteConnectives: (address_to_add: string) => Promise<void>;
  myIonStoreRemoveFavoriteConnectives: (address_to_add: string) => Promise<void>;
  myIonStoreLoadFavoriteConnectives: () => Promise<string[]>;
  myIonStoreFindInFavoriteConnectives: (string_to_search: string) => Promise<boolean>;
}
