import { RewardAnimation } from '../models/rewardAnimations/rewardAnimation';
import { User } from '../models/users/user';
import { BaseService } from './abstract/baseService';

class LocalStorageService extends BaseService {
    public static sharedInstance = new LocalStorageService();

    protected logTag: string = 'LocalStorageService';

    get animations(): RewardAnimation[] | null {
        const animations = sessionStorage.getItem('rewardAnimations');
        if (animations) {
            return JSON.parse(animations);
        } else {
            return null;
        }
    }

    set animations(animations: RewardAnimation[] | null) {
        if (!animations) {
            return;
        }
        const storedValue = JSON.stringify(animations);
        sessionStorage.setItem('rewardAnimations', storedValue);
    }

    get currentUser(): User | null {
        const currentUser = sessionStorage.getItem('currentUser');
        if (currentUser) {
            return JSON.parse(currentUser);
        } else {
            return null;
        }
    }

    set currentUser(currentUser: User | null) {
        if (!currentUser) {
            sessionStorage.removeItem('currentUser');
        } else {
            const storedValue = JSON.stringify(currentUser);
            sessionStorage.setItem('currentUser', storedValue);
        }
    }

    get activePurchases(): string[] | null {
        const activePurchases = sessionStorage.getItem('activePurchases');
        if (activePurchases) {
            return JSON.parse(activePurchases);
        } else {
            return null;
        }
    }

    set activePurchases(activePurchases: string[] | null) {
        if (!activePurchases) {
            sessionStorage.removeItem('activePurchases');
        } else {
            const storedValue = JSON.stringify(activePurchases);
            sessionStorage.setItem('activePurchases', storedValue);
        }
    }
}

export default LocalStorageService.sharedInstance as LocalStorageService;