import { TitleCasePipe } from '@angular/common';
import { ChangeDetectorRef, Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { faTrash } from '@fortawesome/free-solid-svg-icons';
import { NgSelectModule } from '@ng-select/ng-select';
import { initFlowbite } from 'flowbite';
import { KeycloakService } from 'keycloak-angular';
import { NgxCurrencyDirective } from 'ngx-currency';
import { ImageCropperComponent } from 'ngx-image-cropper';
import { QuillModule } from 'ngx-quill';
import { lastValueFrom } from 'rxjs';
import { User } from '../../../../../../bizmatch-server/src/models/db.model';
import { AutoCompleteCompleteEvent, Invoice, Subscription, UploadParams, ValidationMessage, createDefaultUser, emailToDirName } from '../../../../../../bizmatch-server/src/models/main.model';
import { environment } from '../../../../environments/environment';
import { ConfirmationComponent } from '../../../components/confirmation/confirmation.component';
import { ConfirmationService } from '../../../components/confirmation/confirmation.service';
import { ImageCropAndUploadComponent, UploadReponse } from '../../../components/image-crop-and-upload/image-crop-and-upload.component';
import { MessageComponent } from '../../../components/message/message.component';
import { MessageService } from '../../../components/message/message.service';
import { TooltipComponent } from '../../../components/tooltip/tooltip.component';
import { ValidatedCityComponent } from '../../../components/validated-city/validated-city.component';
import { ValidatedInputComponent } from '../../../components/validated-input/validated-input.component';
import { ValidatedQuillComponent } from '../../../components/validated-quill/validated-quill.component';
import { ValidatedSelectComponent } from '../../../components/validated-select/validated-select.component';
import { ValidationMessagesService } from '../../../components/validation-messages.service';
import { GeoService } from '../../../services/geo.service';
import { ImageService } from '../../../services/image.service';
import { LoadingService } from '../../../services/loading.service';
import { SelectOptionsService } from '../../../services/select-options.service';
import { SharedService } from '../../../services/shared.service';
import { SubscriptionsService } from '../../../services/subscriptions.service';
import { UserService } from '../../../services/user.service';
import { SharedModule } from '../../../shared/shared/shared.module';
import { map2User } from '../../../utils/utils';
import { TOOLBAR_OPTIONS } from '../../utils/defaults';
@Component({
  selector: 'app-account',
  standalone: true,
  imports: [
    SharedModule,
    QuillModule,
    NgxCurrencyDirective,
    NgSelectModule,
    ImageCropperComponent,
    ConfirmationComponent,
    ImageCropAndUploadComponent,
    MessageComponent,
    ValidatedInputComponent,
    ValidatedSelectComponent,
    ValidatedQuillComponent,
    ValidatedCityComponent,
    TooltipComponent,
  ],
  providers: [TitleCasePipe],
  templateUrl: './account.component.html',
  styleUrl: './account.component.scss',
})
export class AccountComponent {
  id: string | undefined = this.activatedRoute.snapshot.params['id'] as string | undefined;
  user: User;
  subscriptions: Array<Subscription>;
  userSubscriptions: Array<Subscription> = [];
  companyLogoUrl: string;
  profileUrl: string;
  type: 'company' | 'profile';
  environment = environment;
  editorModules = TOOLBAR_OPTIONS;
  env = environment;
  faTrash = faTrash;
  customerTypes = ['buyer', 'professional'];
  customerSubTypes = ['broker', 'cpa', 'attorney', 'titleCompany', 'surveyor', 'appraiser'];
  quillModules = {
    toolbar: [['bold', 'italic', 'underline', 'strike'], [{ list: 'ordered' }, { list: 'bullet' }], [{ header: [1, 2, 3, 4, 5, 6, false] }], [{ color: [] }, { background: [] }], ['clean']],
  };
  uploadParams: UploadParams;
  validationMessages: ValidationMessage[] = [];
  customerTypeOptions: Array<{ value: string; label: string }> = [];
  customerSubTypeOptions: Array<{ value: string; label: string }> = [];
  tooltipTarget = 'tooltip-areasServed';
  constructor(
    public userService: UserService,
    private subscriptionService: SubscriptionsService,
    private geoService: GeoService,
    public selectOptions: SelectOptionsService,
    private cdref: ChangeDetectorRef,
    private activatedRoute: ActivatedRoute,
    private loadingService: LoadingService,
    private imageUploadService: ImageService,
    private imageService: ImageService,
    private keycloakService: KeycloakService,
    private confirmationService: ConfirmationService,
    private messageService: MessageService,
    private sharedService: SharedService,
    private titleCasePipe: TitleCasePipe,
    private validationMessagesService: ValidationMessagesService,
  ) {}
  async ngOnInit() {
    setTimeout(() => {
      initFlowbite();
    }, 10);
    if (this.id) {
      this.user = await this.userService.getById(this.id);
    } else {
      const token = await this.keycloakService.getToken();
      const keycloakUser = map2User(token);
      const email = keycloakUser.email;
      this.user = await this.userService.getByMail(email);
    }

    this.userSubscriptions = await lastValueFrom(this.subscriptionService.getAllSubscriptions(this.user.id));
    this.profileUrl = this.user.hasProfile ? `${this.env.imageBaseUrl}/pictures/profile/${emailToDirName(this.user.email)}.avif?_ts=${new Date().getTime()}` : `/assets/images/placeholder.png`;
    this.companyLogoUrl = this.user.hasCompanyLogo ? `${this.env.imageBaseUrl}/pictures/logo/${emailToDirName(this.user.email)}.avif?_ts=${new Date().getTime()}` : `/assets/images/placeholder.png`;

    this.customerTypeOptions = this.customerTypes.map(type => ({
      value: type,
      label: this.titleCasePipe.transform(type),
    }));

    this.customerSubTypeOptions = this.customerSubTypes.map(type => ({
      value: type,
      label: this.titleCasePipe.transform(type),
    }));
  }
  ngOnDestroy() {
    this.validationMessagesService.clearMessages(); // Löschen Sie alle bestehenden Validierungsnachrichten
  }
  printInvoice(invoice: Invoice) {}

  async updateProfile(user: User) {
    if (this.user.customerType === 'buyer') {
      const id = this.user.id;
      this.user = createDefaultUser(this.user.email, this.user.firstname, this.user.lastname);
      this.user.customerType = 'buyer';
      this.user.id = id;
      this.imageService.deleteLogoImagesByMail(this.user.email);
      this.imageService.deleteProfileImagesByMail(this.user.email);
    }
    try {
      await this.userService.save(this.user);
      this.messageService.addMessage({ severity: 'success', text: 'Account changes have been persisted', duration: 3000 });
      this.validationMessagesService.clearMessages(); // Löschen Sie alle bestehenden Validierungsnachrichten
      this.validationMessages = [];
    } catch (error) {
      this.messageService.addMessage({
        severity: 'danger',
        text: 'An error occurred while saving the profile',
        duration: 5000,
      });
      if (error.error && Array.isArray(error.error?.message)) {
        this.validationMessagesService.updateMessages(error.error.message);
        this.validationMessages = error.error.message;
      }
    }
  }

  onUploadCompanyLogo(event: any) {
    const uniqueSuffix = '?_ts=' + new Date().getTime();
    this.companyLogoUrl = `${this.env.imageBaseUrl}/pictures/logo/${emailToDirName(this.user.email)}${uniqueSuffix}`;
  }
  onUploadProfilePicture(event: any) {
    const uniqueSuffix = '?_ts=' + new Date().getTime();
    this.profileUrl = `${this.env.imageBaseUrl}/pictures/profile/${emailToDirName(this.user.email)}${uniqueSuffix}`;
  }
  setImageToFallback(event: Event) {
    (event.target as HTMLImageElement).src = `/assets/images/placeholder.png`; // Pfad zum Platzhalterbild
  }

  suggestions: string[] | undefined;

  async search(event: AutoCompleteCompleteEvent) {
    const result = await lastValueFrom(this.geoService.findCitiesStartingWith(event.query));
    this.suggestions = result.map(r => `${r.city} - ${r.state}`).slice(0, 5);
  }
  addLicence() {
    this.user.licensedIn.push({ registerNo: '', state: '' });
  }
  removeLicence() {
    this.user.licensedIn.splice(this.user.licensedIn.length - 1, 1);
  }
  addArea() {
    this.user.areasServed.push({ county: '', state: '' });
  }
  removeArea() {
    this.user.areasServed.splice(this.user.areasServed.length - 1, 1);
  }
  get isProfessional() {
    return this.user.customerType === 'professional';
  }
  uploadCompanyLogo() {
    this.uploadParams = { type: 'uploadCompanyLogo', imagePath: emailToDirName(this.user.email) };
  }
  uploadProfile() {
    this.uploadParams = { type: 'uploadProfile', imagePath: emailToDirName(this.user.email) };
  }
  async uploadFinished(response: UploadReponse) {
    if (response.success) {
      if (response.type === 'uploadCompanyLogo') {
        this.user.hasCompanyLogo = true; //
        this.companyLogoUrl = `${this.env.imageBaseUrl}/pictures/logo/${emailToDirName(this.user.email)}.avif?_ts=${new Date().getTime()}`;
      } else {
        this.user.hasProfile = true;
        this.profileUrl = `${this.env.imageBaseUrl}/pictures/profile/${emailToDirName(this.user.email)}.avif?_ts=${new Date().getTime()}`;
        this.sharedService.changeProfilePhoto(this.profileUrl);
      }
      await this.userService.save(this.user);
    }
  }
  async deleteConfirm(type: 'profile' | 'logo') {
    const confirmed = await this.confirmationService.showConfirmation({ message: `Do you want to delete your ${type === 'logo' ? 'Logo' : 'Profile'} image` });
    if (confirmed) {
      if (type === 'profile') {
        this.user.hasProfile = false;
        await Promise.all([this.imageService.deleteProfileImagesByMail(this.user.email), this.userService.save(this.user)]);
      } else {
        this.user.hasCompanyLogo = false;
        await Promise.all([this.imageService.deleteLogoImagesByMail(this.user.email), this.userService.save(this.user)]);
      }
      this.user = await this.userService.getById(this.user.id);
      // this.messageService.showMessage('Image deleted');
      this.messageService.addMessage({
        severity: 'success',
        text: 'Image deleted.',
        duration: 3000, // 3 seconds
      });
    }
  }
  getValidationMessage(fieldName: string): string {
    const message = this.validationMessages.find(msg => msg.field === fieldName);
    return message ? message.message : '';
  }
  isAdmin() {
    return this.keycloakService.getUserRoles(true).includes('ADMIN');
  }
}
