import React, { createContext, useContext, ReactNode } from 'react';
import { MATCHING_FRENZY_CORRECT_COUNT } from '../../constants';
import { useMyIonMetric } from '.';

const MatchingFrenzyCorrectCountContext = createContext<MatchingFrenzyCorrectCountContextProps | undefined>(undefined);

export const MatchingFrenzyCorrectCountProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
  const { myIonStoreRead, myIonStoreWrite } = useMyIonMetric();

  function Helloworld() {
    console.log('helloworld MatchingFrenzyCorrectCountProvider');
  }

  async function myIonMetricGetMatchingFrenzyCorrectCount() {
    let result = JSON.parse(await myIonStoreRead(MATCHING_FRENZY_CORRECT_COUNT, { count: 0 }));
    return result;
  }

  async function myIonMetricResetMatchingFrenzyCorrectCount() {
    await myIonStoreWrite(MATCHING_FRENZY_CORRECT_COUNT, JSON.stringify({ count: 0 }));
  }

  async function myIonMetricIncMatchingFrenzyCorrectCount() {
    let result = await myIonMetricGetMatchingFrenzyCorrectCount();
    await myIonStoreWrite(MATCHING_FRENZY_CORRECT_COUNT, JSON.stringify({ count: result.count + 1 }));
  }

  async function myIonMetricSetMatchingFrenzyCorrectCount(count: number) {
    await myIonStoreWrite(MATCHING_FRENZY_CORRECT_COUNT, JSON.stringify({ count: count }));
  }

  async function myIonMetricGetMatchingFrenzyCorrectCountIgnore() {
    let result = JSON.parse(await myIonStoreRead(`${MATCHING_FRENZY_CORRECT_COUNT}_ignore`, { count: 0 }));
    return result;
  }

  async function myIonMetricSetMatchingFrenzyCorrectCountIgnore(count: number) {
    await myIonStoreWrite(`${MATCHING_FRENZY_CORRECT_COUNT}_ignore`, JSON.stringify({ count: count }));
  }

  return (
    <MatchingFrenzyCorrectCountContext.Provider
      value={{
        Helloworld,

        // MatchingFrenzyCorrect
        myIonMetricGetMatchingFrenzyCorrectCount,
        myIonMetricResetMatchingFrenzyCorrectCount,
        myIonMetricIncMatchingFrenzyCorrectCount,
        //
        myIonMetricSetMatchingFrenzyCorrectCount,
        myIonMetricGetMatchingFrenzyCorrectCountIgnore,
        myIonMetricSetMatchingFrenzyCorrectCountIgnore
      }}
    >
      {children}
    </MatchingFrenzyCorrectCountContext.Provider>
  );
};

export const useMatchingFrenzyCorrectCount = (): MatchingFrenzyCorrectCountContextProps => {
  const context = useContext(MatchingFrenzyCorrectCountContext);
  if (!context) {
    throw new Error('useMatchingFrenzyCorrectCountContext must be used within a MyProvider');
  }
  return context;
};

interface MatchingFrenzyCorrectCountContextProps {
  Helloworld: () => void;
  // MatchingFrenzyCorrect
  myIonMetricGetMatchingFrenzyCorrectCount: () => Promise<{ count: number }>;
  myIonMetricResetMatchingFrenzyCorrectCount: () => Promise<void>;
  myIonMetricIncMatchingFrenzyCorrectCount: () => Promise<void>;
  //
  myIonMetricSetMatchingFrenzyCorrectCount: (count: number) => Promise<void>;
  myIonMetricGetMatchingFrenzyCorrectCountIgnore: () => Promise<{ count: number }>;
  myIonMetricSetMatchingFrenzyCorrectCountIgnore: (count: number) => Promise<void>;
}
