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

const ListenPracticeTimeSpent = createContext<ListenPracticeTimeSpentProps | undefined>(undefined);

export const ListeningPracticeTimeSpentProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
  // TODO create namespace for LISTENING_PRACTICE_TIME_SPENT ignore

  const { myIonStoreRead, myIonStoreWrite } = useMyIonMetric();

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

  async function myIonMetricGetListeningPracticeTimeSpent() {
    let result = JSON.parse(await myIonStoreRead(LISTENING_PRACTICE_TIME_SPENT, { time_spent_s: 0 }));
    return result;
  }

  async function myIonMetricResetListeningPracticeTimeSpent() {
    await myIonStoreWrite(LISTENING_PRACTICE_TIME_SPENT, JSON.stringify({ time_spent_s: 0 }));
  }

  async function myIonMetricIncListeningPracticeTimeSpent(time_spent_s: number) {
    let result = await myIonMetricGetListeningPracticeTimeSpent();
    await myIonStoreWrite(
      LISTENING_PRACTICE_TIME_SPENT,
      JSON.stringify({ time_spent_s: result.time_spent_s + time_spent_s })
    );
  }

  async function myIonMetricSetListeningPracticeProgress(num: number) {
    await myIonStoreWrite(
      //
      LISTENING_PRACTICE_TIME_SPENT,
      JSON.stringify({ time_spent_s: num })
    );
  }

  async function myIonMetricGetListeningPracticeProgressIgnore() {
    let result = JSON.parse(await myIonStoreRead(LISTENING_PRACTICE_TIME_SPENT + '_ignore', { time_spent_s: 0 }));
    return result;
  }

  async function myIonMetricSetListeningPracticeProgressIgnore(time_spent_s: number) {
    await myIonStoreWrite(LISTENING_PRACTICE_TIME_SPENT + '_ignore', JSON.stringify({ time_spent_s: time_spent_s }));
  }

  return (
    <ListenPracticeTimeSpent.Provider
      value={{
        Helloworld,
        myIonMetricGetListeningPracticeTimeSpent,
        myIonMetricIncListeningPracticeTimeSpent,
        myIonMetricResetListeningPracticeTimeSpent,
        //
        myIonMetricSetListeningPracticeProgress,
        myIonMetricGetListeningPracticeProgressIgnore,
        myIonMetricSetListeningPracticeProgressIgnore
      }}
    >
      {/*  */}
      {children}
    </ListenPracticeTimeSpent.Provider>
  );
};

export const useListeningPracticeTimeSpent = (): ListenPracticeTimeSpentProps => {
  const context = useContext(ListenPracticeTimeSpent);
  if (!context) {
    throw new Error('useListenPracticeTimeSpent must be used within a MyProvider');
  }
  return context;
};

interface ListenPracticeTimeSpentProps {
  Helloworld: () => void;
  //
  // ListeningPractice
  myIonMetricGetListeningPracticeTimeSpent: () => Promise<{ time_spent_s: number }>;
  myIonMetricIncListeningPracticeTimeSpent: (time_spent_s: number) => Promise<void>;
  myIonMetricResetListeningPracticeTimeSpent: () => Promise<void>;
  //
  myIonMetricSetListeningPracticeProgress: (time_spent_s: number) => Promise<void>;
  myIonMetricGetListeningPracticeProgressIgnore: () => Promise<{ time_spent_s: number }>;
  myIonMetricSetListeningPracticeProgressIgnore: (time_spent_s: number) => Promise<void>;
}
