import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { ActivatedRoute} from '@angular/router';
import { HttpErrorResponse } from '@angular/common/http';
import { ToastrService } from 'ngx-toastr';
import { environment } from 'src/environments/environment';
import { LoginEnvironment } from './login_environment';
import { AuthService } from '../../services/auth.service'; // Import the new AuthService
import { LoginRequest } from '../../models/auth/auth.model'; // Import LoginRequest DTO


@Component({
  selector: 'app-login-page',
  templateUrl: './login-page.component.html',
  styleUrls: ['./login-page.component.scss']
})
export class LoginPageComponent implements OnInit {

  loginEnvironment = LoginEnvironment;
  showLogin2 = true;
 

  // loginEnvironment: LoginEnvironment = {
  //   companyName: 'io8.dev',
  //   imagePath: '../../../../assets/images/new.png',
  //   fpass: 'forgot password',
  //   isSignup: true,
  //   // template:'',
  // };


  // loginEnvironment: ExtendedLoginEnvironment = {
  //   companyName:  'io8.dev',
  //   imagePath: '../../../../assets/images/new.png',
  //   fpass: 'Forgot Password?',
  //   isSignup: true  // or false based on your requirement
  // };
  

  email = '';
  password = '';
  isError = false;

  model: LoginRequest = { email: '', password: '' }; // Initialize with LoginRequest DTO
  errMsg: string = '';
  constructor(
    private router: Router,
    private route:ActivatedRoute,
    private authService: AuthService, // Use AuthService
    private toastr: ToastrService,
  ) { }

  ngOnInit() {
    this.authService.removeToken(); // Ensure no old token is present

    this.loginEnvironment["imagePath"] = !this.loginEnvironment.loginImageURL ? "../../../../assets/images/new.png" : this.loginEnvironment.loginImageURL;

    console.log('Login Environment:', this.loginEnvironment);
  }

  getWrapperClass(): string {
    if (this.loginEnvironment.templateNo === 'Template 1') {
      return 'login-wrapper tamplate1';

    } else if (this.loginEnvironment.templateNo === 'Template 2') {
      return 'login-wrapper tamplate2';
    } else if (this.loginEnvironment.templateNo === '<templateNo>') {
      return 'login-wrapper tamplate1'; // Default class or empty string if no match
    }
  }

  onLogin() {
    this.isError = false; // Reset error state
    this.errMsg = ''; // Clear previous error message

    this.authService.login(this.model)
      .subscribe(resp => {
        if (resp.token) {
          this.authService.saveToken(resp.token);
          this.router.navigate(['/main']); // Redirect to main dashboard or home page
        } else {
          this.isError = true;
          this.errMsg = 'Login failed: No token received.';
        }
      },
        (errResponse: HttpErrorResponse) => {
          this.isError = true; // Set error state to true
          switch (errResponse.status) {
            case 401:
              this.errMsg = 'Email or password is incorrect';
              break;
            case 404:
              this.errMsg = 'Service not found';
              break;
            case 408:
              this.errMsg = 'Request Timedout';
              break;
            case 500:
              this.errMsg = 'Internal Server Error';
              break;
            default:
              this.errMsg = 'Server Error';
              break;
          }
          this.toastr.error(this.errMsg, 'Login Failed'); // Display error using Toastr
        }
      );

  }
  goaccount(){
  }
  goforgotpass(){
    this.router.navigate(["../forgotpass"], { relativeTo: this.route });
  }


}
