import type { OpenData } from './classes.main.opendata.js';
import * as plugins from './plugins.js';

/**
 * the HandlesRegister exposed as a class
 */
export class HandelsRegister {
  private openDataRef: OpenData;

  public smartbrowserInstance = new plugins.smartbrowser.SmartBrowser();

  constructor(openDataRef: OpenData) {
    this.openDataRef = openDataRef;
  }

  public async start() {
    await this.smartbrowserInstance.start();
  }

  public async stop() {
    await this.smartbrowserInstance.stop();
  }

  /**
   * Search for a company by name
   */
  public async getDataForCompany(companyNameArg: string) {
    const page = await this.smartbrowserInstance.headlessBrowser.newPage();
    await page.setViewport({ width: 1920, height: 1080 });
    await page.goto('https://www.handelsregister.de/');
    await page.evaluate(() => {
      const elements = Array.from(document.querySelectorAll('.ui-menuitem-text > span'));
      const targetElement = elements.find((el) => el.textContent?.trim() === 'Normal search');
      if (targetElement) {
        (targetElement as HTMLElement).click();
      }
    });

    try {
      // Wait for the textarea to appear
      await page.waitForSelector('#form\\:schlagwoerter', { timeout: 5000 });

      // Enter text into the textarea using page.evaluate
      const inputText = companyNameArg;
      await page.evaluate((text) => {
        const textarea = document.querySelector<HTMLTextAreaElement>('#form\\:schlagwoerter');
        if (textarea) {
          textarea.value = text; // Set the value
          // Trigger the change event manually if required
          const event = new Event('change', { bubbles: true });
          textarea.dispatchEvent(event);
        }
      }, inputText);

      console.log('Text entered successfully!');
    } catch (error) {
      console.error('Failed to find or enter text into the textarea:', error);
    }

    try {
      // Wait for the radio button's label to appear
      await page.waitForSelector('label[for="form:schlagwortOptionen:0"]', { timeout: 5000 });

      // Click the label to select the radio button
      await page.evaluate(() => {
        const label = document.querySelector<HTMLLabelElement>(
          'label[for="form:schlagwortOptionen:0"]'
        );
        if (label) {
          label.click();
        }
      });

      console.log('Radio button clicked successfully!');
    } catch (error) {
      console.error('Failed to find or click the radio button:', error);
    }

    try {
      // Wait for the button with the text "Find" to appear
      await page.waitForSelector('span.ui-button-text.ui-c', { timeout: 5000 });

      // Locate and click the button using its text
      await page.evaluate(() => {
        const buttons = Array.from(document.querySelectorAll('span.ui-button-text.ui-c'));
        const targetButton = buttons.find((button) => button.textContent?.trim() === 'Find');
        if (targetButton) {
          const parentButton = targetButton.closest('button') || targetButton;
          (parentButton as HTMLElement).click();
        }
      });

      console.log('Find button clicked successfully!');
    } catch (error) {
      console.error('Failed to find or click the "Find" button:', error);
    }

    await page.waitForSelector('#ergebnissForm\\:selectedSuchErgebnisFormTable_data', {
      timeout: 10000,
    });

    const businessRecords = await page.evaluate(() => {
      const rows = document.querySelectorAll(
        '#ergebnissForm\\:selectedSuchErgebnisFormTable_data > tr'
      );
      const records = [];

      rows.forEach((row) => {
        const nameElement = row.querySelector('td.ui-panelgrid-cell span.marginLeft20');
        const cityElement = row.querySelector('td.ui-panelgrid-cell.sitzSuchErgebnisse span');
        const statusElement = row.querySelector('td.ui-panelgrid-cell span.verticalText');
        const registrationCourtElement = row.querySelector(
          'td.ui-panelgrid-cell.fontTableNameSize'
        );

        const name = nameElement?.textContent?.trim();
        const city = cityElement?.textContent?.trim();
        const status = statusElement?.textContent?.trim();
        const registrationCourt = registrationCourtElement?.textContent?.trim();

        // Push parsed data into records array
        records.push({
          name,
          city,
          registrationCourt,
          businessType: status,
        });
      });

      return records;
    });

    await page.close();

    // Finally, we return an object, which triggers a JSON file download
    return businessRecords;
  }
}