import { Component, OnInit } from '@angular/core';
import { DetailsFormBase } from '@objects-view/details-form-base';
import { IPredefinedReplyCategoryModel } from '@fleio-api/plugins/tickets/model/predefined-reply-category.model';
import { UntypedFormBuilder, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { PredefinedReplyCategoryApiService } from '@fleio-api/plugins/tickets/predefined-reply-category-api.service';
import { EMPTY, Observable, of } from 'rxjs';
import { IActionResult } from '@objects-view/interfaces/actions/action-result';
import { IPredefinedReplyModel } from '@fleio-api/plugins/tickets/model/predefined-reply.model';
import { ThemingService } from '@shared/ui/theming/theming.service';

@Component({
  selector: 'app-predefined-reply-edit-form',
  templateUrl: './predefined-reply-edit-form.component.html',
  styleUrls: ['./predefined-reply-edit-form.component.scss']
})
export class PredefinedReplyEditFormComponent extends DetailsFormBase<IPredefinedReplyCategoryModel> implements OnInit {
  tinyMCEOptions = null;
  predefinedReply: IPredefinedReplyModel;
  predefinedReplyForm = this.formBuilder.group({
    title: ['', Validators.required],
    content: ['', Validators.required],
  });


  constructor(
    private formBuilder: UntypedFormBuilder, private config: ConfigService,
    private predefinedReplyCategoryApiService: PredefinedReplyCategoryApiService, private router: Router,
    private themingService: ThemingService,
  ) {
    super();
  }

  ngOnInit() {
    super.ngOnInit();
    if (this.objectController) {
      this.objectController.actionCallback = () => this.savePredefinedReply();
      this.predefinedReply = this.objectController?.additionalObjects?.predefinedReply;
    }
    if (this.config.current) {
      this.tinyMCEOptions = this.themingService.getTinyMceOptions();
    }
    if (this.predefinedReply) {
      this.predefinedReplyForm.patchValue(this.predefinedReply);
    }
  }

  handleTinyKeyDown(event: any) {
    if (event && event.event && event.event.ctrlKey && event.event.keyCode === 13) {
      event.event.preventDefault();
      this.savePredefinedReply();
    }
  }

  private savePredefinedReply(): Observable<IActionResult> {
    const value = this.predefinedReplyForm.value as any;
    value.category = this.object.id;
    if (this.predefinedReply) {
      value.id = this.predefinedReply.id;
    }

    this.validate();
    if (this.formGroup.invalid) {
      this.displayControlErrors();
      return EMPTY;
    }

    this.objectController.loading = true;
    this.predefinedReplyCategoryApiService.savePredefinedReply(
      this.object.id, value,
    ).subscribe({
      next: () => {
        this.router.navigateByUrl(
          this.config.getPrevUrl(`tickets/predefined-replies/category/${this.object.id}`)
        ).catch(() => {
        });
      }, error: error => {
        this.objectController.loading = false;
        if (error.error) {
          this.setErrors(error.error);
          return EMPTY;
        } else {
          throw error;
        }
      }
    });

    return of(null);
  }
}
