'use strict';

const httpService = require('./httpService-6377985c.js');

const { state, onChange, reset } = httpService.createStore({
  user: {
    FirstName: '',
    LastName: '',
    ProfileText: '',
    ProfileImage: '',
    IsVerified: false,
    Roles: []
  },
  loaded: false,
  enrollmentList: [],
});
const userStore = { state, onChange, reset };

class StudentService {
  constructor() { }
  static getInstance() {
    if (!StudentService.instance) {
      StudentService.instance = new StudentService();
    }
    return StudentService.instance;
  }
  async enrollClass(enrollInput) {
    let result = await httpService.HttpService.http.post('api/v1/Student/EnrollClass', enrollInput);
    if (result) {
      userStore.state.enrollmentList = [
        ...userStore.state.enrollmentList,
        result.Data
      ];
    }
    return result;
  }
  async leaveClass(leaveInput) {
    let result = await httpService.HttpService.http.post('api/v1/Student/LeaveClass', leaveInput);
    if (result) {
      userStore.state.enrollmentList = userStore.state.enrollmentList.filter((item) => item.Id != result.Data);
    }
    return result;
  }
  async fetchUserConfiguration() {
    let result = await httpService.HttpService.http.get('api/v1/Student/UserConfiguration');
    if (result) {
      userStore.state.enrollmentList = result.Data.EnrollmentList;
      userStore.state.user.FirstName = result.Data.FirstName;
      userStore.state.user.LastName = result.Data.LastName;
      userStore.state.user.ProfileText = result.Data.ProfileText;
      userStore.state.user.ProfileImage = result.Data.ProfileImage;
      userStore.state.user.IsVerified = result.Data.IsVerified;
      userStore.state.user.Roles = result.Data.Roles;
    }
    return result;
  }
  async updateProfile(updateProfileInput) {
    let result = await httpService.HttpService.http.post('api/Account/UpdateProfile', updateProfileInput);
    // if (result) {
    //     userStore.state.user.FirstName = result.Data.FirstName;
    //     userStore.state.user.LastName = result.Data.LastName;
    //     userStore.state.user.ProfileText = result.Data.ProfileText;
    //     userStore.state.user.ProfileImage = result.Data.ProfileImage;
    // }
    return result;
  }
  async submitFeedback(feedbackInput) {
    let result = await httpService.HttpService.http.post('api/v1/Feedback', feedbackInput);
    return result;
  }
}

exports.StudentService = StudentService;
exports.userStore = userStore;
