import { Component, OnInit } from "@angular/core";
import { UserService } from "../../shared/user.service";
import { Router } from "@angular/router";
import { NgForm } from "@angular/forms";

@Component({
  selector: 'app-developerstats',
  templateUrl: './developerstats.component.html',
  styleUrls: ['./developerstats.component.css']
})
export class DeveloperstatsComponent implements OnInit {
  userDetails;
  emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  showSucessMessage: boolean;
  serverErrorMessages: string;
  email:any;

  constructor(private userService: UserService, private router: Router) {}

  //This function gets called when the page loads
  //Returns the user profile
  ngOnInit() {
    this.userService.getUserProfile().subscribe(
      res => {
        this.userDetails = res["user"];
        this.email = '';
      },
      err => {}
    );
  }

    /**
   * This function gets called when a user submits a developer's email.
   * It will take the information taken from the form (information from the input boxes in the HTML)
   * and send it to the backend server to be sent to the MongoDB
   */
  onSubmit(form: NgForm) {
    this.userService.updateAccountType(form.value).subscribe(
      res => {
        this.showSucessMessage = true;
        setTimeout(() => (this.showSucessMessage = false), 4000);
        location.reload();
      },
      err => {
        if (err.status === 422) {
          this.serverErrorMessages = err.error.join("<br/>");
        } else {
          this.serverErrorMessages =
            "Something went wrong.Please contact admin.";
        }

      }
    );
  }

  //Redirects to the profile page
  profile() {
    if (!this.userService.isLoggedIn()) {
      this.router.navigate(["/login"]);
    } else {
      this.router.navigate(["/userprofile"]);
    }
  }

  //Allows you to logout from the Developer Stats page
  onLogout() {
    this.userService.deleteToken();
    this.router.navigate(["/login"]);
  }

  //This function redirects the user to the queyhistory page
  queryHistory() {
    if (!this.userService.isLoggedIn()) {
      this.router.navigate(["/login"]);
    } else {
      this.router.navigate(["/queryhistory"], { queryParams: { page: '1' } });
    }
  }

  //This function redirects the user to the Query Interactions page
  feedbackInteractions() {
    if (!this.userService.isLoggedIn()) {
      this.router.navigate(["/login"]);
    } else {
      this.router.navigate(["/feedbackinteractions"]);
    }
  }

  //This function redirects the user to the Article Ratings page
  articleInformation() {
      if (!this.userService.isLoggedIn()) {
        this.router.navigate(["/login"]);
      } else {
        this.router.navigate(["/articleinfo"]);
      }
  }

  //This function redirects the user to the feedbackHistory page
  feedbackHistory() {
    if (!this.userService.isLoggedIn()) {
      this.router.navigate(["/login"]);
    } else {
      this.router.navigate(["/feedbackhistory"]);
    }
  }
  
  //This function redirects the user to the queryinformation page
  queryInformation(){
      if (!this.userService.isLoggedIn()) {
        this.router.navigate(["/login"]);
      } else {
        this.router.navigate(["/queryinformation"]);
      }
    }
}

