import { Component, OnInit } from '@angular/core';
import {AppService} from "../../services/app.service";
import {ActivatedRoute, Router} from "@angular/router";
import {UserService} from "../../services/user.service";
import {MessageService} from "primeng/api";
import {TranslateService} from "@ngx-translate/core";
import {HttpErrorResponse} from "@angular/common/http";

@Component({
  selector: 'app-account-activate',
  templateUrl: './account-activate.component.html',
  styleUrls: ['./account-activate.component.css']
})
export class AccountActivateComponent implements OnInit {
  public loading: boolean = true;
  errorMessages: any[] = [];
  severity = "warn";

  constructor(
      public app: AppService,
      private router: Router,
      private userService: UserService,
      private messageService: MessageService,
      private activatedRoute: ActivatedRoute,
      private i18Service: TranslateService
  ) { }

  ngOnInit(): void {
    if (this.app.isLoggedIn())
      this.userService.logout().subscribe(
          _ => {
            this.app.removeAuthorization();
          },
          err => {
            console.error(err);
            this.app.removeAuthorization();
          }
      );
    
    this.activatedRoute.params.subscribe(params => {
      this.errorMessages = [];
      if (params && params.code) {

        this.userService
            .activate(params.code)
            .subscribe(
                (response: any) => {
                  this.loading = false;
                  if (response.success) {
                    this.severity = "success";
                    this.i18Service.get(['ACCOUNT_ACTIVATION.CHECK', 'ACCOUNT_ACTIVATION.SUCCESS']).subscribe(o => {
                      this.errorMessages.push({
                        name: o["ACCOUNT_ACTIVATION.CHECK"],
                        message: response.messages && response.messages.length > 0 ? response.messages.join(", ") : o["ACCOUNT_ACTIVATION.SERVER_ERROR"]
                      });
                    })
                  }
                  else {
                    this.severity = "warn";
                    this.i18Service.get(['ACCOUNT_ACTIVATION.CHECK', 'ACCOUNT_ACTIVATION.SERVER_ERROR']).subscribe(o => {
                      this.errorMessages.push({
                        name: o["ACCOUNT_ACTIVATION.CHECK"],
                        message: response.messages && response.messages.length > 0 ? response.messages.join(", ") : o["ACCOUNT_ACTIVATION.SERVER_ERROR"]
                      });
                    })
                  }
                },
                (err: HttpErrorResponse) => {
                  this.severity = "warn";
                  this.i18Service.get(['ACCOUNT_ACTIVATION.CHECK', 'ACCOUNT_ACTIVATION.SERVER_ERROR']).subscribe(o => {
                    this.errorMessages.push({
                      name: o["ACCOUNT_ACTIVATION.CHECK"],
                      message: err.message ? err.message : o["ACCOUNT_ACTIVATION.SERVER_ERROR"]
                    });
                  })
                  this.loading = false;
                }
            );
      }
      else {
        this.loading = false;
        this.i18Service.get(['ACCOUNT_ACTIVATION.CODE', 'ACCOUNT_ACTIVATION.CODE_MISSING']).subscribe(o => {
          this.errorMessages.push({
            name: o['ACCOUNT_ACTIVATION.CODE'],
            message: o['ACCOUNT_ACTIVATION.CODE_MISSING']
          });
        });
      }
    });
  }

  goToLoginPage() {
    this.router.navigateByUrl("/login")
  }
}
