/*!
 * Copyright © 2005-2025 Hyland Software, Inc. and its affiliates. All rights reserved.
 *
 * Alfresco Example Content Application
 *
 * This file is part of the Alfresco Example Content Application.
 * If the software was purchased under a paid Alfresco license, the terms of
 * the paid license agreement will prevail. Otherwise, the software is
 * provided under the following open source license terms:
 *
 * The Alfresco Example Content Application is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * The Alfresco Example Content Application is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
 */

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { EditRuleDialogOptions, EditRuleDialogUiComponent } from './edit-rule-dialog.ui-component';
import { RuleDetailsUiComponent } from './rule-details.ui-component';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { NoopTranslateModule, UnitTestingUtils } from '@alfresco/adf-core';
import { of, timer } from 'rxjs';
import { AlfrescoApiService, AlfrescoApiServiceMock } from '@alfresco/adf-content-services';
import { Rule } from '../model/rule.model';

describe('EditRuleDialogSmartComponent', () => {
  let fixture: ComponentFixture<EditRuleDialogUiComponent>;
  let component: EditRuleDialogUiComponent;
  let unitTestingUtils: UnitTestingUtils;

  const dialogRef = {
    close: jasmine.createSpy('close'),
    open: jasmine.createSpy('open')
  };

  const getDialogSubmit = (): HTMLButtonElement => unitTestingUtils.getByDataAutomationId('edit-rule-dialog-submit').nativeElement;
  const getDialogTitle = (): HTMLDivElement => unitTestingUtils.getByDataAutomationId('edit-rule-dialog-title').nativeElement;

  const setupBeforeEach = (dialogOptions: EditRuleDialogOptions = { actionDefinitions$: of([]), parameterConstraints$: of([]) }) => {
    TestBed.configureTestingModule({
      imports: [NoopTranslateModule, EditRuleDialogUiComponent],
      providers: [
        { provide: MatDialogRef, useValue: dialogRef },
        { provide: MAT_DIALOG_DATA, useValue: dialogOptions },
        { provide: AlfrescoApiService, useClass: AlfrescoApiServiceMock }
      ]
    });

    fixture = TestBed.createComponent(EditRuleDialogUiComponent);
    fixture.detectChanges();
    component = fixture.componentInstance;
    unitTestingUtils = new UnitTestingUtils(fixture.debugElement);
  };

  describe('No dialog options passed / indifferent', () => {
    beforeEach(() => {
      setupBeforeEach();
    });

    it('should activate the submit button only when a valid state is received', async () => {
      const ruleDetails = unitTestingUtils.getByDirective(RuleDetailsUiComponent).componentInstance as RuleDetailsUiComponent;
      ruleDetails.formValidationChanged.emit(true);

      fixture.detectChanges();
      // timer needed to wait for the next tick to avoid ExpressionChangedAfterItHasBeenCheckedError
      await timer(1).toPromise();
      fixture.detectChanges();
      expect(getDialogSubmit().disabled).toBeFalsy();
      ruleDetails.formValidationChanged.emit(false);

      fixture.detectChanges();
      await timer(1).toPromise();
      fixture.detectChanges();
      expect(getDialogSubmit().disabled).toBeTruthy();
    });

    it('should show a "create" label in the title', () => {
      expect(getDialogTitle().innerText.trim()).toBe('ACA_FOLDER_RULES.EDIT_RULE_DIALOG.CREATE_TITLE');
    });

    it('should show a "create" label in the submit button', () => {
      expect(getDialogSubmit().innerText.trim()).toBe('ACA_FOLDER_RULES.EDIT_RULE_DIALOG.CREATE');
    });

    it('should set correct title and submitLabel for create mode', () => {
      expect(component.title).toBe('ACA_FOLDER_RULES.EDIT_RULE_DIALOG.CREATE_TITLE');
      expect(component.submitLabel).toBe('ACA_FOLDER_RULES.EDIT_RULE_DIALOG.CREATE');
    });
  });

  describe('With dialog options passed', () => {
    const dialogOptions: EditRuleDialogOptions = {
      model: {
        id: 'rule-id'
      },
      actionDefinitions$: of([]),
      parameterConstraints$: of([])
    };

    beforeEach(() => {
      setupBeforeEach(dialogOptions);
    });

    it('should show an "update" label in the title', () => {
      expect(getDialogTitle().innerText.trim()).toBe('ACA_FOLDER_RULES.EDIT_RULE_DIALOG.UPDATE_TITLE');
    });

    it('should show an "update" label in the submit button', () => {
      expect(getDialogSubmit().innerText.trim()).toBe('ACA_FOLDER_RULES.EDIT_RULE_DIALOG.UPDATE');
    });

    it('should set correct title and submitLabel for update mode', () => {
      expect(component.title).toBe('ACA_FOLDER_RULES.EDIT_RULE_DIALOG.UPDATE_TITLE');
      expect(component.submitLabel).toBe('ACA_FOLDER_RULES.EDIT_RULE_DIALOG.UPDATE');
    });
  });

  it('should emit submitted event with form value when onSubmit is called', () => {
    setupBeforeEach();
    let emittedValue: Partial<Rule> | undefined;

    const testFormValue: Partial<Rule> = {
      id: 'test-id',
      name: 'Test Rule',
      description: 'Test description'
    };

    component.submitted.subscribe((value) => {
      emittedValue = value;
    });

    component.formValue = testFormValue;
    component.onSubmit();

    expect(emittedValue).toEqual(testFormValue);
  });
});
