import { Component, OnInit } from '@angular/core';
import { ServicesService } from '../../services.service';
import { NgxSpinnerService } from 'ngx-spinner';
import { Router } from '@angular/router';
import { LoginflowService } from '../../data/loginflow.service';
import { NotificationService } from 'src/app/services/notification/notification.service';
import { AuthserviceService } from 'src/app/authservice.service';

@Component({
  selector: 'app-usersetting',
  templateUrl: './usersetting.component.html',
  styleUrls: ['./usersetting.component.scss'],
})
export class UsersettingComponent implements OnInit {
  checkSwitch: boolean;
  oberdue: boolean = false;
  live_events: boolean = false;
  instructor: boolean = false;
  chat: boolean = false;

  constructor(
    public LoginflowService: LoginflowService,
    private router: Router,
    public service: ServicesService,
    private spinner: NgxSpinnerService,
    private notiService: NotificationService,
    private auth: AuthserviceService,
  ) { }

  ngOnInit(): void {
    this.getNotificationSettings();
  }
  async getNotificationSettings() {
    let user_id = localStorage.getItem('CisspUserId');
    this.spinner.show();
    await this.auth.getAuthenticatedUser().getSession(async (err, session) => {
      if (err) {
        await this.spinner.hide();
        this.router.navigate(['/login']);
      } else {
        var token = session
        await this.notiService.getNotificationSettings(user_id, token).then(async (data) => {
          if (data[0].status == 200) {
            var list = JSON.parse(<string>data[0].json);
            if (list.data.length > 0) {
              this.oberdue = list.data[0].oberdue;
              this.live_events = list.data[0].live_events;
              this.instructor = list.data[0].instructor;
              this.chat = list.data[0].chat;
            } else {
              this.oberdue = false;
              this.live_events = false;
              this.instructor = false;
              this.chat = false;
            }
          }
          await this.spinner.hide();
        });
      }
    });
  }
  async setNotificationSettings() {
    this.spinner.show();
    let user_id = localStorage.getItem('CisspUserId');
    await this.auth.getAuthenticatedUser().getSession(async (err, session) => {
      if (err) {
        await this.spinner.hide();
        this.router.navigate(['/login']);
      } else {
        var token = session
        await this.notiService.setNotificationSettings(user_id, this.oberdue, this.live_events, this.instructor, this.chat, token).then(async (data) => {
          if (data[0].status == 200) {
            this.getNotificationSettings();
          }
          await this.spinner.hide();
        });
      }
    });
  }
  async resetSetting() {
    await this.getNotificationSettings();
  }
  async ChangeSwitch() {
    this.checkSwitch = !this.checkSwitch;
  }
  versions_getUserById(user_id) {
    this.spinner.show();

    this.LoginflowService.versions_getUserById(user_id).then((data) => {
      var list = JSON.parse(<string>data[0].json);
      if (list.data[0].opt_in == '1') {
        this.checkSwitch = false;
        this.spinner.hide();
      } else {
        this.checkSwitch = true;
        this.spinner.hide();
      }
    });
  }
  async save() {
    if (this.checkSwitch) {
      this.spinner.show();
      let user_id = localStorage.getItem('CisspUserId');
      await this.LoginflowService.UpdateUserOptIn('0', user_id).then(
        async (data) => {
          if (data[0].status == 200) {
            this.service.ShowSuccess('Saved successfully!');

            this.spinner.hide();
          } else {
            this.spinner.hide();
          }
        }
      );
    } else {
      this.spinner.show();
      let user_id = localStorage.getItem('CisspUserId');
      await this.LoginflowService.UpdateUserOptIn('1', user_id).then(
        async (data) => {
          if (data[0].status == 200) {
            this.service.ShowSuccess('Saved successfully!');
            this.spinner.hide();
          } else {
            this.spinner.hide();
          }
        }
      );
    }
  }
}