import { BasePage } from '@/manager/shared/base/BasePage';
import { InvoiceDetailsCustomerCard } from '@/manager/modules/invoice/components/InvoiceDetailsCustomerCard';
import { InvoiceDetailsGeneralCard } from '@/manager/modules/invoice/components/InvoiceDetailsGeneralCard';
import { InvoiceDetailsPaymentsCard } from '@/manager/modules/invoice/components/InvoiceDetailsPaymentsCard';
import { InvoiceIssueCreditNoteDialog } from '@/manager/modules/invoice/views/InvoiceIssueCreditNoteDialog';
import { InvoiceListPage } from '@/manager/modules/invoice/views/InvoiceListPage';
import { InvoiceMarkAsPaidDialog } from '@/manager/modules/invoice/views/InvoiceMarkAsPaidDialog';
import { InvoiceMarkAsVoidDialog } from '@/manager/modules/invoice/views/InvoiceMarkAsVoidDialog';
import { getPageHeaderStatusHost, getPageHeaderBackButtonHost } from '@/manager/shared/utils/locator-utils';

export class InvoiceDetailsPage extends BasePage {
  readonly baseStatus = getPageHeaderStatusHost(this.header);
  readonly creditNoteIssuedStatus = this.header.getByTestId('base-view-header-status-credit-note-issued');
  readonly backButton = getPageHeaderBackButtonHost(this.header);
  readonly actionMenu = this.host.getByTestId('invoice-action-menu');
  readonly actionMenuButton = this.header.getByTestId('invoice-action-button');
  readonly actionMenuIssueCreditNoteItem = this.actionMenu.getByTestId('invoice-action-issue-credit-note');
  readonly actionMenuMarkAsPaidItem = this.actionMenu.getByTestId('invoice-action-switch-status-paid');
  readonly actionMenuMarkAsVoidItem = this.actionMenu.getByTestId('invoice-action-switch-status-void');
  readonly generalCard = new InvoiceDetailsGeneralCard(this.body);
  readonly customerCard = new InvoiceDetailsCustomerCard(this.body);
  readonly paymentsCard = new InvoiceDetailsPaymentsCard(this.body);

  async goto(id: number): Promise<void> {
    await this.host.goto(`/invoices/${id}`);
  }

  async reload(): Promise<void> {
    await this.host.reload();
  }

  async openInvoiceIssueCreditNoteDialog(): Promise<InvoiceIssueCreditNoteDialog> {
    await this.actionMenuButton.click();
    await Promise.all([
      this.host.waitForURL('**/invoices/*/issue-credit-note'),
      this.actionMenuIssueCreditNoteItem.click(),
    ]);
    return new InvoiceIssueCreditNoteDialog(this.host);
  }

  async openInvoiceMarkAsPaidDialog(): Promise<InvoiceMarkAsPaidDialog> {
    await this.actionMenuButton.click();
    await this.actionMenuMarkAsPaidItem.click();
    return new InvoiceMarkAsPaidDialog(this.host);
  }

  async openInvoiceMarkAsVoidDialog(): Promise<InvoiceMarkAsVoidDialog> {
    await this.actionMenuButton.click();
    await this.actionMenuMarkAsVoidItem.click();
    return new InvoiceMarkAsVoidDialog(this.host);
  }

  async getInvoiceId(): Promise<string> {
    return (await this.title.innerText()).trim();
  }

  async returnToInvoiceListPage(): Promise<InvoiceListPage> {
    await Promise.all([this.host.waitForURL('/invoices'), this.backButton.click()]);
    return new InvoiceListPage(this.host);
  }
}
