// FILSTI: app\src\main\java\com\kbs\kbsintranett\UserManager.java
package com.kbs.kbsintranett;

import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

/**
 * UserManager fungerer som en global sesjon for appen.
 * Den holder på informasjon om innlogget bruker, rettigheter og autentiserings-cookie.
 */
public class UserManager {
    private static UserManager instance;

    // Google Data
    private String userDisplayName;
    private String userEmail;
    private String googleIdToken;
    private String photoUrl;

    // WordPress Data
    private int userId;
    private String userRole;
    private String currentCookie;

    // Extended Info
    private String firstName;
    private String lastName;
    private String stilling;
    private String mobiltelefon;

    // FCM Token (Push)
    private String fcmToken;

    // NYTT:
    private List<String> writeableCalendars = new ArrayList<>();

    private UserManager() {}

    public static synchronized UserManager getInstance() {
        if (instance == null) {
            instance = new UserManager();
        }
        return instance;
    }

    public void setUserData(String name, String email, String token, @Nullable String photoUrl) {
        this.userDisplayName = name;
        this.userEmail = email;
        this.googleIdToken = token;
        this.photoUrl = photoUrl;
    }

    public void setExtendedUserInfo(String firstName, String lastName, String stilling, String mobiltelefon) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.stilling = stilling;
        this.mobiltelefon = mobiltelefon;
    }

    public void setWriteableCalendars(List<String> calendars) {
        this.writeableCalendars = calendars != null ? calendars : new ArrayList<>();
    }

    public List<String> getWriteableCalendars() {
        return writeableCalendars;
    }

    public void setCookie(String cookie) { this.currentCookie = cookie; }
    public void setUserRole(String role) { this.userRole = role; }
    public void setUserId(int id) { this.userId = id; }

    public String getUserDisplayName() { return userDisplayName != null ? userDisplayName : ""; }
    public String getUserEmail() { return userEmail != null ? userEmail : ""; }
    public String getGoogleIdToken() { return googleIdToken; }
    public String getPhotoUrl() { return photoUrl; }
    public String getCookie() { return currentCookie; }
    public String getUserRole() { return userRole != null ? userRole : "subscriber"; }
    public int getUserId() { return userId; }
    public String getFirstName() { return firstName != null ? firstName : ""; }
    public String getLastName() { return lastName != null ? lastName : ""; }
    public String getStilling() { return stilling != null ? stilling : ""; }
    public String getMobiltelefon() { return mobiltelefon != null ? mobiltelefon : ""; }

    public void setFcmToken(String token) { this.fcmToken = token; }
    public String getFcmToken() { return fcmToken; }

    public boolean isLoggedIn() { return userEmail != null && !userEmail.isEmpty(); }
    public boolean isAdmin() { return "administrator".equalsIgnoreCase(userRole); }
    public boolean isEditorOrAbove() {
        return userRole != null && (userRole.equalsIgnoreCase("administrator") || userRole.equalsIgnoreCase("editor"));
    }

    public void logout() {
        userDisplayName = null;
        userEmail = null;
        googleIdToken = null;
        photoUrl = null;
        userRole = null;
        currentCookie = null;
        userId = 0;
        firstName = null;
        lastName = null;
        stilling = null;
        mobiltelefon = null;
        writeableCalendars.clear();
        // Vi sletter ikke fcmToken ved logout, da enheten fortsatt er den samme
    }
}