import * as React from "react";
import * as _ from "lodash";
import { shallow } from "enzyme";
import {
    render,
    renderIntoDocument,
    cleanup,
    wait,
    fireEvent,
} from "react-testing-library";

import Pager from "../components/Pager";
import { PAGER_ELEMENT } from "../components/Pager/constants";

beforeEach(cleanup);

describe("Pager Calcutions", () => {
    test("01 - Pager getCurrentPageSet", () => {
        const pagerProps = {
            pagesToShow: 5,
            totalItems: 100,
            itemsPerPage: 10,
            onPageChange: () => {},
        };
        const pager: Pager = new Pager(pagerProps);

        const currentPageSet1: number = pager.getCurrentPageSet(4);
        expect(currentPageSet1).toBe(0);
        const currentPageSet2: number = pager.getCurrentPageSet(5);
        expect(currentPageSet2).toBe(1);
        const currentPageSet3: number = pager.getCurrentPageSet(35);
        expect(currentPageSet3).toBe(7);
        const currentPageSet4: number = pager.getCurrentPageSet(40);
        expect(currentPageSet4).toBe(8);
    });

    test("02 - Pager getTotalPageSet", () => {
        const pagerProps = {
            pagesToShow: 7,
            totalItems: 211,
            itemsPerPage: 10,
            onPageChange: () => {},
        };
        const pager: Pager = new Pager(pagerProps);

        const totalPageSet: number = pager.getTotalPageSets();
        expect(totalPageSet).toBe(4);
    });

    test("03 - Pager getPagesInCurrentPageSet", () => {
        const pagerProps = {
            pagesToShow: 7,
            totalItems: 211,
            itemsPerPage: 10,
            onPageChange: () => {},
        };
        const pager: Pager = new Pager(pagerProps);

        const pagesInCurrentPageSet: number[] = pager.getPagesInCurrentPageSet(
            12,
            1,
            4,
            211
        );
        _.times(7, i => {
            expect(pagesInCurrentPageSet[i]).toBe(8 + i);
        });
    });

    test("04 - Pager getTotalPages", () => {
        const pagerProps = {
            pagesToShow: 5,
            totalItems: 101,
            itemsPerPage: 10,
            onPageChange: () => {},
        };
        const pager: Pager = new Pager(pagerProps);

        const totalPages: number = pager.getTotalPages();
        expect(totalPages).toBe(11);
    });

    test("05 - Pager onPagerItemClicked nextPage", async () => {
        const onPageChange: () => void = jest.fn();
        const pager = (
            <Pager
                pagesToShow={7}
                totalItems={211}
                itemsPerPage={10}
                onPageChange={onPageChange}
            />
        );
        const wrapper = shallow(pager);
        wrapper.setState({ currentPageNumber: 8 });
        await wait(() => expect(wrapper.state("currentPageNumber")).toBe(8));
        wrapper.instance().onPagerItemClicked(PAGER_ELEMENT.NEXT_PAGE);

        expect(wrapper.state().currentPageNumber).toEqual(9);
    });

    test("06 - Pager onPagerItemClicked prevPage", async () => {
        const onPageChange: () => void = jest.fn();
        const pager = (
            <Pager
                pagesToShow={7}
                totalItems={211}
                itemsPerPage={10}
                onPageChange={onPageChange}
            />
        );
        const wrapper = shallow(pager);
        wrapper.setState({ currentPageNumber: 8 });
        await wait(() => expect(wrapper.state("currentPageNumber")).toBe(8));
        wrapper.instance().onPagerItemClicked(PAGER_ELEMENT.PREVIOUS_PAGE);

        expect(wrapper.state().currentPageNumber).toEqual(7);
    });

    test("07 - Pager onPagerItemClicked firstPage", async () => {
        const onPageChange: () => void = jest.fn();
        const pager = (
            <Pager
                pagesToShow={7}
                totalItems={211}
                itemsPerPage={10}
                onPageChange={onPageChange}
            />
        );
        const wrapper = shallow(pager);
        wrapper.setState({ currentPageNumber: 8 });
        await wait(() => expect(wrapper.state("currentPageNumber")).toBe(8));
        wrapper.instance().onPagerItemClicked(PAGER_ELEMENT.FIRST_PAGE);

        expect(wrapper.state().currentPageNumber).toEqual(0);
    });

    test("08 - Pager onPagerItemClicked lastPage", async () => {
        const onPageChange: () => void = jest.fn();
        const pager = (
            <Pager
                pagesToShow={7}
                totalItems={211}
                itemsPerPage={10}
                onPageChange={onPageChange}
            />
        );
        const wrapper = shallow(pager);
        wrapper.setState({ currentPageNumber: 8 });
        await wait(() => expect(wrapper.state("currentPageNumber")).toBe(8));
        wrapper.instance().onPagerItemClicked(PAGER_ELEMENT.LAST_PAGE);

        expect(wrapper.state().currentPageNumber).toEqual(21);
    });

    test("09 - Pager onPagerItemClicked nextSet", async () => {
        const onPageChange: () => void = jest.fn();
        const pager = (
            <Pager
                pagesToShow={7}
                totalItems={211}
                itemsPerPage={10}
                onPageChange={onPageChange}
            />
        );
        const wrapper = shallow(pager);
        wrapper.setState({ currentPageNumber: 8 });
        await wait(() => expect(wrapper.state("currentPageNumber")).toBe(8));
        wrapper.instance().onPagerItemClicked(PAGER_ELEMENT.NEXT_SET);

        expect(wrapper.state().currentPageNumber).toEqual(14);
    });

    test("10 - Pager onPagerItemClicked prevSet", async () => {
        const onPageChange: () => void = jest.fn();
        const pager = (
            <Pager
                pagesToShow={7}
                totalItems={211}
                itemsPerPage={10}
                onPageChange={onPageChange}
            />
        );
        const wrapper = shallow(pager);
        wrapper.setState({ currentPageNumber: 8 });
        await wait(() => expect(wrapper.state("currentPageNumber")).toBe(8));
        wrapper.instance().onPagerItemClicked(PAGER_ELEMENT.PREVIOUS_SET);

        expect(wrapper.state().currentPageNumber).toEqual(0);
    });
});

describe("Pager Renders", () => {
    test("11 - Pager getCurrentPageSet", () => {
        const onPageChange: () => void = jest.fn();

        const { container, getAllByTestId } = render(
            <Pager
                pagesToShow={7}
                totalItems={211}
                itemsPerPage={10}
                onPageChange={onPageChange}
            />
        );
        const laded: string[] = getAllByTestId("pager_item_component").map(
            ass => ass.textContent.trim()
        );
        _.forEach(laded, (l, i) => {
            expect(parseInt(l)).toBe(i + 1);
        });
    });
});
