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

const ConnectivesRevisionCorrectCount = createContext<ConnectivesRevisionCorrectCountProps | undefined>(undefined);

export const ConnectivesRevisionCorrectCountProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
  const NS_KEY_IGNORE = `${CONNECTIVES_REVISION_CORRECT_COUNT}_ignore`;

  const { myIonStoreRead, myIonStoreWrite } = useMyIonMetric();

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

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

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

  async function myIonMetricIncConnectivesRevisionCorrectCount() {
    let result = await myIonMetricGetConnectivesRevisionCorrectCount();
    await myIonStoreWrite(CONNECTIVES_REVISION_CORRECT_COUNT, JSON.stringify({ count: result.count + 1 }));
  }

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

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

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

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

        // ConnectivesRevisionCorrect
        myIonMetricGetConnectivesRevisionCorrectCount,
        myIonMetricResetConnectivesRevisionCorrectCount,
        myIonMetricIncConnectivesRevisionCorrectCount,
        //
        myIonMetricSetConnectivesRevisionCorrectCount,
        myIonMetricGetConnectivesRevisionCorrectCountIgnore,
        myIonMetricSetConnectivesRevisionCorrectCountIgnore
      }}
    >
      {children}
    </ConnectivesRevisionCorrectCount.Provider>
  );
};

export const useConnectivesRevisionCorrectCount = (): ConnectivesRevisionCorrectCountProps => {
  const context = useContext(ConnectivesRevisionCorrectCount);
  if (!context) {
    throw new Error('useConnectivesRevisionCorrectCount must be used within a MyProvider');
  }
  return context;
};

interface ConnectivesRevisionCorrectCountProps {
  Helloworld: () => void;
  // ConnectivesRevision
  myIonMetricGetConnectivesRevisionCorrectCount: () => Promise<{ count: number }>;
  myIonMetricResetConnectivesRevisionCorrectCount: () => Promise<void>;
  myIonMetricIncConnectivesRevisionCorrectCount: () => Promise<void>;
  //
  myIonMetricSetConnectivesRevisionCorrectCount: (count: number) => Promise<void>;
  myIonMetricGetConnectivesRevisionCorrectCountIgnore: () => Promise<{ count: number }>;
  myIonMetricSetConnectivesRevisionCorrectCountIgnore: (count: number) => Promise<void>;
}
