import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ProposeCantoPage } from './propose-canto.page';
import { ToastController, PopoverController, NavController, AngularDelegate } from '@ionic/angular';
import { CantiService } from '../../services/canti.service';
import { MyCantiService } from '../../services/my-canti.service';
import { PlaylistService } from '../../services/playlist.service';
import { ThemeService } from '../../services/theme.service';
import { ActivatedRoute, Router } from '@angular/router';
import { LyricsParserService } from '../../services/lyrics-parser.service';
import { of } from 'rxjs';

describe('ProposeCantoPage', () => {
  let component: ProposeCantoPage;
  let fixture: ComponentFixture<ProposeCantoPage>;

  beforeEach(() => {
    const cantiServiceMock = {
      indiceLiturgico: () => [],
      indiceTematico: () => [],
      canti: () => []
    };
    const myCantiServiceMock = {
      myCanti: () => []
    };
    const playlistServiceMock = {
      remoteCustomSongs: () => [],
      activePlaylistId: () => null
    };
    const themeServiceMock = {
      highContrast: () => false
    };
    const activatedRouteMock = {
      queryParams: of({})
    };
    const routerMock = {
      navigate: jasmine.createSpy('navigate')
    };
    const lyricsParserMock = {
      parseAccordi: () => []
    };
    const navCtrlMock = {};
    const toastControllerMock = {};
    const popoverControllerMock = {};
    const angularDelegateMock = {};

    TestBed.configureTestingModule({
      providers: [
        { provide: CantiService, useValue: cantiServiceMock },
        { provide: MyCantiService, useValue: myCantiServiceMock },
        { provide: PlaylistService, useValue: playlistServiceMock },
        { provide: ThemeService, useValue: themeServiceMock },
        { provide: ActivatedRoute, useValue: activatedRouteMock },
        { provide: Router, useValue: routerMock },
        { provide: LyricsParserService, useValue: lyricsParserMock },
        { provide: NavController, useValue: navCtrlMock },
        { provide: ToastController, useValue: toastControllerMock },
        { provide: PopoverController, useValue: popoverControllerMock },
        { provide: AngularDelegate, useValue: angularDelegateMock }
      ]
    });

    fixture = TestBed.createComponent(ProposeCantoPage);
    component = fixture.componentInstance;
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  describe('convertEnglishChordToItalian', () => {
    it('should convert simple English chords to Italian', () => {
      expect(component.convertEnglishChordToItalian('C')).toBe('DO');
      expect(component.convertEnglishChordToItalian('D')).toBe('RE');
      expect(component.convertEnglishChordToItalian('E')).toBe('MI');
      expect(component.convertEnglishChordToItalian('F')).toBe('FA');
      expect(component.convertEnglishChordToItalian('G')).toBe('SOL');
      expect(component.convertEnglishChordToItalian('A')).toBe('LA');
      expect(component.convertEnglishChordToItalian('B')).toBe('SI');
    });

    it('should convert English chords with accidentals and modifiers', () => {
      expect(component.convertEnglishChordToItalian('C#m7')).toBe('DO#m7');
      expect(component.convertEnglishChordToItalian('Bb')).toBe('SIb');
      expect(component.convertEnglishChordToItalian('F#m')).toBe('FA#m');
      expect(component.convertEnglishChordToItalian('Faug')).toBe('FAaug');
      expect(component.convertEnglishChordToItalian('Fadd9')).toBe('FAadd9');
    });

    it('should keep Italian chords unchanged', () => {
      expect(component.convertEnglishChordToItalian('DO')).toBe('DO');
      expect(component.convertEnglishChordToItalian('RE#m7')).toBe('RE#m7');
      expect(component.convertEnglishChordToItalian('FA#')).toBe('FA#');
      expect(component.convertEnglishChordToItalian('SIb')).toBe('SIb');
    });

    it('should handle slash chords correctly', () => {
      expect(component.convertEnglishChordToItalian('C/E')).toBe('DO/MI');
      expect(component.convertEnglishChordToItalian('D/F#')).toBe('RE/FA#');
      expect(component.convertEnglishChordToItalian('F#m7/A#')).toBe('FA#m7/LA#');
    });
  });

  describe('sanitizeOcrChord', () => {
    it('should convert H to # when following a chord root', () => {
      expect(component.sanitizeOcrChord('CH')).toBe('C#');
      expect(component.sanitizeOcrChord('DHm7')).toBe('D#m7');
      expect(component.sanitizeOcrChord('FH#')).toBe('F#');
      expect(component.sanitizeOcrChord('FAH')).toBe('FA#');
      expect(component.sanitizeOcrChord('SOLH7')).toBe('SOL#7');
      expect(component.sanitizeOcrChord('FH#H')).toBe('F#');
      expect(component.sanitizeOcrChord('FH#H/AH#')).toBe('F#/A#');
      expect(component.sanitizeOcrChord('C#H-')).toBe('C#-');
      expect(component.sanitizeOcrChord('G#H#')).toBe('G#');
    });

    it('should convert 0 to O for DO and SOL roots', () => {
      expect(component.sanitizeOcrChord('D0')).toBe('DO');
      expect(component.sanitizeOcrChord('D0#')).toBe('DO#');
      expect(component.sanitizeOcrChord('S0L')).toBe('SOL');
    });

    it('should handle slash chords with sanitization', () => {
      expect(component.sanitizeOcrChord('CH/EH')).toBe('C#/E#');
      expect(component.sanitizeOcrChord('D0/FH#')).toBe('DO/F#');
    });
  });
});
