import React from 'react';
import {acceptInvitation, createInvitation} from "../../API/dpass";
import dayjs from "dayjs";
import {AddInvitationParams} from "../../models/DPass/NewInvitation";
import {faker} from '@faker-js/faker';
import {toastSuccess} from "../../services/toaster";

const Invitations = () => {

    /**
     * random day from today and next 30 days
     */
    const day = () => {
        return dayjs(faker.date.soon(15)).format('DD-MM-YYYY')
    }


    /**
     * tuple of from and to hours
     */
    const fromTo = () => {
        const from = range(0, 22)
        const to = range(from + 1, 23)
        return [
            dayjs().hour(from).format('HH:mm'),
            dayjs().hour(to).format('HH:mm')]
    }


    const visitorFactory = () => {
        return {
            visitor_email: faker.internet.email(),
            visitor_firstname: faker.name.firstName(),
            visitor_lastname: faker.name.lastName(),
            visitor_phone: faker.phone.number('69########')
        }
    }

    /**
     * creates an array of 15 visitors objects
     */
    const visitors = Array(15)
        .fill(null)
        .map(() => visitorFactory())

    const buildings = ["2-κτίριο 2_5-INRASTES", '3-κτίριο 3_5-INRASTES', "5-κτίριο 5_4-INN", "6-κτίριο 6_3-INPP", "8-κτίριο 8_4-INN", "9-κτίριο 9_5-INRASTES"]

    /**
     * Generates fake params which have to posted
     */
    const singleVisitFactory = (): AddInvitationParams => {

        const visits = range(1, 4)
        const hours = Array(visits)
            .fill(null)
            .map(() => fromTo())

        return {
            type: 'single',
            visits_dates: Array(visits)
                .fill(null)
                .map(() => day()),
            visits_from: hours.map(h => h[0]),
            visits_to: hours.map(h => h[1]),
            building_institute: faker.helpers.arrayElement(buildings),
            room_no: range(1, 4).toString(),
            message_to_visitor: faker.lorem.lines(range(1, 3)),
            message_to_gate: faker.lorem.lines(range(1, 3)),
            visitors_emails: [faker.helpers.arrayElement(visitors).visitor_email],
            title: faker.random.words(range(1, 5)),
            group_name: faker.random.words(range(1, 5)),
            mass_title: faker.random.words(range(1, 5)),
            behalf_user:faker.random.words(range(1, 5)),
            behalf_signature:faker.random.words(range(1, 5))
        }

    }

    /**
     * Generates fake params which have to posted
     */
    const massVisitFactory = (): AddInvitationParams => {

        const visits = 1
        const hours = Array(visits)
            .fill(null)
            .map(() => fromTo())

        return {
            type: 'mass',
            visits_dates: Array(visits)
                .fill(null)
                .map(() => day()),
            visits_from: hours.map(h => h[0]),
            visits_to: hours.map(h => h[1]),
            building_institute: faker.helpers.arrayElement(buildings),
            room_no: range(1, 4).toString(),
            message_to_visitor: faker.lorem.lines(range(1, 3)),
            message_to_gate: faker.lorem.lines(range(1, 3)),
            visitors_emails: faker.helpers.arrayElements(visitors.map(v => v.visitor_email), range(2, 5)),
            title: faker.random.words(range(1, 5)),
            group_name: faker.random.words(range(1, 5)),
            mass_title: faker.random.words(range(1, 5))
        }

    }

    // visitors_emails: faker.helpers.arrayElements(visitors.map(v => v.visitor_email), range(2, 5)),
    const range = (from: number, to: number) => {
        return faker.datatype.number({min: from, max: to})
    }


    const fakeInvSingle = async () => {

        const total = 80;
        const block_size = 20;

        /**
         * we make 100 invitations in back sizes of 25 (4 * 25)
         */
        const invitationProms = []
        for (let i = 0; i < 20; i++) {

            const params = singleVisitFactory()

            invitationProms.push(createInvitation(params))
        }
        const resArr = await Promise.all(invitationProms)

        // @ts-ignore
        const acceptInvProms = []
        resArr.forEach(res => {
            // @ts-ignore
            const visitor = visitors.find(v => v.visitor_email === res.data.invitation.visitor.email)
            // @ts-ignore
            visitor.invitation_id=res.data.invitation.id
            // @ts-ignore
            acceptInvProms.push(acceptInvitation(visitor))


        })
        // @ts-ignore
        const res2Arr = await Promise.all(acceptInvProms)

        toastSuccess('CTREATED 20 FAKE mass INVITATIONS')
    }

    /**
     * auto accept invations
     * this should be sent on visitors emails
     * and accepted from different form
     */

    const fakeInvMass = async () => {


        /**
         * we make 100 invitations in back sizes of 25 (4 * 25)
         */
        const invitationProms = []
        for (let i = 0; i < 5; i++) {

            const params = massVisitFactory()

            invitationProms.push(createInvitation(params))
        }
        const resArr = await Promise.all(invitationProms)

        // @ts-ignore
        const acceptInvProms = []
        resArr.forEach(res => {
            // @ts-ignore
            res.data.invitations.map(inv => {
                const visitor = visitors.find(v => v.visitor_email === inv.visitor.email)
                // @ts-ignore
                visitor.invitation_id=inv.id
                // @ts-ignore
                acceptInvProms.push(acceptInvitation(visitor))

            })


        })
        // @ts-ignore
        const res2Arr = await Promise.all(acceptInvProms)

        toastSuccess('CTREATED 100 FAKE SINGLE INVITATIONS')
    }


    return (
        <div>
            <h1>lets make some fake invitations</h1>
            <button onClick={fakeInvSingle}
            >GENERATE SINGLE INVITATIONS (25)
            </button>

            <button onClick={fakeInvMass}
            >GENERATE MASS INVITATIONS (5)
            </button>
        </div>
    );
}

export default Invitations;