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

@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(
      private router: Router,
      private userService: UserService,
      private authService: AuthService,
      private activatedRoute: ActivatedRoute,
      private i18Service: TranslateService
  ) { }

  ngOnInit(): void {
    if (this.authService.isLoggedIn())
      this.userService.logout();
    
    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")
  }
}
