import AsyncStorage from '@react-native-async-storage/async-storage';

// Storage keys definition
const TIMER_KEYS = {
  REPS: 'timer_reps',
  WORK_TIME: 'timer_work_time',
  REST_TIME: 'timer_rest_time',
  PREPARATION_TIME: 'timer_preparation_time'
};

// Store interface
export interface TimerState {
  reps: number;
  workTime: number;
  restTime: number;
  preparationTime: number;
}

// Default values
const DEFAULT_STATE: TimerState = {
  reps: 1,
  workTime: 0,
  restTime: 0,
  preparationTime: 3
};

// Store management class
class TimerStore {
  // Method to save repetitions
  async saveReps(reps: number): Promise<void> {
    try {
      await AsyncStorage.setItem(TIMER_KEYS.REPS, reps.toString());
    } catch (error) {
      console.error('Error saving repetitions:', error);
    }
  }

  async savePreparationTime(preparationTime: number): Promise<void> {
    try {
      await AsyncStorage.setItem(TIMER_KEYS.PREPARATION_TIME, preparationTime.toString());
    } catch (error) {
      console.error('Error saving preparation time:', error);
    }
  }

  // Method to save work time
  async saveWorkTime(workTime: number): Promise<void> {
    try {
      await AsyncStorage.setItem(TIMER_KEYS.WORK_TIME, workTime.toString());
    } catch (error) {
      console.error('Error saving work time:', error);
    }
  }

  // Method to save rest time
  async saveRestTime(restTime: number): Promise<void> {
    try {
      await AsyncStorage.setItem(TIMER_KEYS.REST_TIME, restTime.toString());
    } catch (error) {
      console.error('Error saving rest time:', error);
    }
  }

  // Method to save complete state
  async saveState(state: TimerState): Promise<void> {
    try {
      await Promise.all([
        this.saveReps(state.reps),
        this.saveWorkTime(state.workTime),
        this.saveRestTime(state.restTime),
        this.savePreparationTime(state.preparationTime)
      ]);
    } catch (error) {
      console.error('Error saving state:', error);
    }
  }

  // Method to retrieve repetitions
  async getReps(): Promise<number> {
    try {
      const value = await AsyncStorage.getItem(TIMER_KEYS.REPS);
      return value !== null ? parseInt(value, 10) : DEFAULT_STATE.reps;
    } catch (error) {
      console.error('Error retrieving repetitions:', error);
      return DEFAULT_STATE.reps;
    }
  }

  async getPreparationTime(): Promise<number> {
    try {
      const value = await AsyncStorage.getItem(TIMER_KEYS.PREPARATION_TIME);
      return value !== null ? parseInt(value, 10) : DEFAULT_STATE.preparationTime;
    } catch (error) {
      console.error('Error retrieving preparation time:', error);
      return DEFAULT_STATE.preparationTime;
    }
  }

  // Method to retrieve work time
  async getWorkTime(): Promise<number> {
    try {
      const value = await AsyncStorage.getItem(TIMER_KEYS.WORK_TIME);
      return value !== null ? parseInt(value, 10) : DEFAULT_STATE.workTime;
    } catch (error) {
      console.error('Error retrieving work time:', error);
      return DEFAULT_STATE.workTime;
    }
  }

  // Method to retrieve rest time
  async getRestTime(): Promise<number> {
    try {
      const value = await AsyncStorage.getItem(TIMER_KEYS.REST_TIME);
      return value !== null ? parseInt(value, 10) : DEFAULT_STATE.restTime;
    } catch (error) {
      console.error('Error retrieving rest time:', error);
      return DEFAULT_STATE.restTime;
    }
  }

  // Method to retrieve complete state
  async getState(): Promise<TimerState> {
    try {
      const [reps, workTime, restTime, preparationTime] = await Promise.all([
        this.getReps(),
        this.getWorkTime(),
        this.getRestTime(),
        this.getPreparationTime()
      ]);

      return {
        reps,
        workTime,
        restTime,
        preparationTime
      };
    } catch (error) {
      console.error('Error retrieving state:', error);
      return DEFAULT_STATE;
    }
  }
}

// Export a singleton instance
export const timerStore = new TimerStore();