import { environment } from "../../src/environments/environment";
export class ClientSubmissionPage {
    baseUrl: string = environment.api.baseUrl;
    constructor() {
        // Get form data
        cy.intercept('POST', `${this.baseUrl}/form-data`, { fixture: 'form-data.json' })
            .as('getFormData');

        cy.intercept('GET', `${this.baseUrl}/company?page=1&limit=10`, { fixture: 'companies.json' })
            .as('getCompanies');

        // Get medicine 
        cy.intercept('GET', `${this.baseUrl}/medicine?page=1&limit=30&search=P`, { fixture: 'medicine.json' })
            .as('getMedicine');

        // Get dosage types
        cy.intercept('GET', `${this.baseUrl}/medicine/1/dosage-type?page=1&limit=10`, {
            fixture: 'dosage-types.json'
        })
            .as('getDosageTypes');

        // Get package types 
        cy.intercept('GET', `${this.baseUrl}/medicine/${1}/dosage-type/${1}/package?page=${1}&limit=${10}`, {
            fixture: 'package-types.json'
        })
            .as('getMedicinePackageType');
    }
    public visit(): void {
        cy.visit('/portal/submit');
    }

    public isAuthenticated(): void {
        cy.byTestId('welcome-message').should('contain', 'Welcome');
    }

    public nextStep(): void {
        cy.byTestId('next-step').first().click({ force: true });
    }

    public confirmAndSave(): void {
        cy.byTestId('save-button').click({ force: true });
        cy.byTestId('confirm-button').click({ force: true });
    }

    public cancel(): void {
        cy.byTestId('cancel-button').click({ force: true });
    }

    public addMedicine(total: number) {

        let i = 0;

        while (i < total) {
            cy.byTestId('add-medicine-name')
                .clear({ force: true })
                .type('P', { force: true });

            cy.wait('@getMedicine');
            cy.wait(1500);

            cy.byTestId('add-medicine-autocompletion-list')
                .get('mat-option')
                .first()
                .click({ force: true });

            cy.wait('@getDosageTypes');
            cy.wait(1500);

            cy.byTestId('add-medicine-dosage-type')
                .click({ force: true })
                .get('mat-option')
                .first()
                .click({ force: true });

            cy.wait('@getMedicinePackageType');
            cy.wait(1500);

            cy.byTestId('add-medicine-package-type')
                .click({ force: true })
                .get('mat-option')
                .first()
                .click({ force: true });

            cy.byTestId('add-medicine-quantity')
                .clear({ force: true })
                .type('10', { force: true })

            cy.byTestId('add-medicine-add-button').click({ force: true });

            cy.wait(1500);

            i++;
        }
    }

    public uploadPrescriptionFile(): void {
        cy.byTestId('file-uploader-file')
            .attachFile('sample.pdf');

        cy.byTestId("file-uploader-upload-button")
            .click({ force: true });
    }

    public submitForIndividual(): void {

        cy.wait('@getFormData');
        cy.wait('@getCompanies');

        cy.byTestId('entity-type')
            .click()
            .get('mat-option')
            .contains('Individual')
            .click();

        this.nextStep();

        cy.scrollTo('top');
        cy.wait(1500);

        cy.byTestId('individual-title')
            .click()
            .get('mat-option')
            .contains('Mrs')
            .click({ force: true });

        cy.byTestId('individual-initials')
            .clear({ force: true })
            .type('BS', { force: true });

        cy.byTestId('individual-first-name')
            .clear()
            .type('Beth');

        cy.byTestId('individual-last-name')
            .clear()
            .type('Smith');

        cy.byTestId('individual-gender')
            .click({ force: true })
            .get('mat-option')
            .contains('Female')
            .click({ force: true });

        cy.byTestId('individual-date-of-birth')
            .clear()
            .type('3/4/1991');

        cy.byTestId('individual-phone-number')
            .clear()
            .type('0782123789')

        cy.byTestId('individual-type')
            .click({ force: true })
            .get('mat-option')
            .contains('Patient')
            .click({ force: true });


        cy.byTestId('individual-number-of-dependants')
            .clear({ force: true })
            .type('10', { force: true });


        cy.byTestId('individual-country')
            .click({ force: true })
            .get('mat-option')
            .contains('South Africa')
            .click({ force: true });

        cy.byTestId('individual-province')
            .click({ force: true })
            .get('mat-option')
            .contains('Gauteng')
            .click({ force: true });

        cy.byTestId('individual-address-line-1')
            .clear({ force: true })
            .type('1234 Fake Street', { force: true });

        cy.byTestId('individual-address-line-2')
            .clear({ force: true })
            .type('5678 Another Fake Street', { force: true });

        cy.byTestId('individual-building-apt-number')
            .clear({ force: true })
            .type('12', { force: true });

        cy.byTestId('individual-postcode')
            .clear({ force: true })
            .type('1234', { force: true });

        cy.byTestId('individual-delivery-type')
            .click({ force: true })
            .get('mat-option')
            .contains('Delivery')
            .click({ force: true });

        this.nextStep();

        this.addMedicine(3);

        this.confirmAndSave();

        this.uploadPrescriptionFile();
    }

    public submitForCompany(): void {

        cy.wait('@getFormData');

        cy.byTestId('entity-type')
            .click()
            .get('mat-option')
            .contains('Company')
            .click();

        this.nextStep();

        cy.byTestId('company-name')
            .clear({ force: true })
            .type('Microsoft', { force: true });

        cy.wait('@getCompanies');
        cy.wait(1500);

        cy.byTestId('company-email')
            .clear({ force: true })
            .type('user@microsoft.com', { force: true });

        cy.byTestId('company-email-alt')
            .clear({ force: true })
            .type('user@outlook.com', { force: true });

        cy.byTestId('company-practice-number')
            .clear({ force: true })
            .type('12345678', { force: true });

        cy.byTestId('company-vat-reg-number')
            .clear({ force: true })
            .type('12345678', { force: true });

        cy.byTestId('company-type')
            .click({ force: true })
            .get('mat-option')
            .contains('Doctor\'s Practice')
            .click({ force: true });

        this.nextStep();

        cy.byTestId('individual-title')
            .click({ force: true })
            .get('mat-option')
            .contains('Mrs')
            .click({ force: true });

        cy.byTestId('individual-initials')
            .clear({ force: true })
            .type('BS', { force: true });

        cy.byTestId('individual-first-name')
            .clear()
            .type('Beth');

        cy.byTestId('individual-last-name')
            .clear()
            .type('Smith');

        cy.byTestId('individual-gender')
            .click({ force: true })
            .get('mat-option')
            .contains('Female')
            .click({ force: true });

        cy.byTestId('individual-date-of-birth')
            .clear()
            .type('3/4/1991');

        cy.byTestId('individual-phone-number')
            .clear()
            .type('0782123789')

        cy.byTestId('individual-type')
            .click({ force: true })
            .get('mat-option')
            .contains('Patient')
            .click({ force: true });


        cy.byTestId('individual-number-of-dependants')
            .clear({ force: true })
            .type('10', { force: true });


        cy.byTestId('individual-country')
            .click({ force: true })
            .get('mat-option')
            .contains('South Africa')
            .click({ force: true });

        cy.byTestId('individual-province')
            .click({ force: true })
            .get('mat-option')
            .contains('Gauteng')
            .click({ force: true });

        cy.byTestId('individual-address-line-1')
            .clear({ force: true })
            .type('1234 Fake Street', { force: true });

        cy.byTestId('individual-address-line-2')
            .clear({ force: true })
            .type('5678 Another Fake Street', { force: true });

        cy.byTestId('individual-building-apt-number')
            .clear({ force: true })
            .type('12', { force: true });

        cy.byTestId('individual-postcode')
            .clear({ force: true })
            .type('1234', { force: true });

        cy.byTestId('individual-delivery-type')
            .click({ force: true })
            .get('mat-option')
            .contains('Delivery')
            .click({ force: true });

        this.nextStep();

        this.addMedicine(3);

        this.confirmAndSave();

        this.uploadPrescriptionFile();
    }
}