const colors = [
    {
        name: '(default)',
        id: 'default',
    },
    {
        name: 'Gray',
        id: '#666666',
    },
    {
        name: 'Green',
        id: '#008e00',
    },
    {
        name: 'Red',
        id: '#d74f29',
    },
    {
        name: 'Blue',
        id: '#004dc3',
    },
    {
        name: 'Yellow',
        id: '#eab71e',
    },
];

async function create(start, end) {
    const { data: blocks } = await fetch("Default.aspx/GetBlocks", { headers: { 'Content-Type': 'application/json' } })
        .then(response => response.json())
        .then(data => JSON.parse(data.d));

    const form = [
        {
            name: 'Day',
            id: 'day',
            type: 'date',
            dateFormat: 'M/d/yyyy',
        },
        {
            type: 'select',
            id: 'start',
            name: 'Start',
            options: blocks,
        },
        {
            type: 'select',
            id: 'duration',
            name: 'Duration (number of blocks)',
            options: [
                {
                    name: '1',
                    id: 1,
                },
                {
                    name: '2',
                    id: 2,
                },
                {
                    name: '3',
                    id: 3,
                },
                {
                    name: '4',
                    id: 4,
                },
                {
                    name: '5',
                    id: 5,
                },
                {
                    name: '6',
                    id: 6,
                },
                {
                    name: '7',
                    id: 7,
                },
            ],
        },
        {
            type: 'select',
            id: 'color',
            name: 'Color',
            options: colors,
        },
        {
            name: 'Description',
            id: 'description',
            type: 'text',
        },
    ];

    const data = {
        day: new DayPilot.Date(start),
        start: new DayPilot.Date(start).getHours(),
        duration: new DayPilot.Date(end).getHours() - new DayPilot.Date(start).getHours(),
        color: "default"
    };

    const options = {
        focus: "description"
    };

    const modal = await DayPilot.Modal.form(form, data, options);
    if (modal.canceled) {
        return;
    }

    fetch("Default.aspx/Create",
            {
                headers: {
                    'Content-Type': 'application/json'
                },
                method: "POST",
                body: JSON.stringify(modal.result)
            })
        .then(r => r.json())
        .then(r => {
            const data = JSON.parse(r.d);
            dp.commandCallBack("refresh", { message: data.message });
        });
}

async function edit(e) {

    const assignment = await fetch(`Default.aspx/GetAssignment?id=${e.id()}`, { headers: { 'Content-Type': 'application/json' } })
        .then(response => response.json())
        .then(data => JSON.parse(data.d));

    const form = [
        {
            type: 'date',
            id: 'start',
            name: 'Start',
            dateFormat: 'd/M/yyyy hh:mm tt',
            disabled: true
        },
        {
            type: 'date',
            id: 'end',
            name: 'End',
            dateFormat: 'd/M/yyyy hh:mm tt',
            disabled: true
        },
        {
            type: 'select',
            id: 'color',
            name: 'Color',
            options: colors,
        },
        {
            name: 'Description',
            id: 'description',
            type: 'text',
        },
    ];

    const modal = await DayPilot.Modal.form(form, assignment);

    if (modal.canceled) {
        return;
    }

    fetch("Default.aspx/Update",
            {
                headers: {
                    'Content-Type': 'application/json'
                },
                method: "POST",
                body: JSON.stringify(modal.result)
            })
        .then(r => r.json())
        .then(r => {
            const data = JSON.parse(r.d);
            dp.commandCallBack("refresh", { message: data.message });
        });
}

async function editBlock(e) {
    console.log("e", e);
    var start = e.hours;

    const data = await fetch(`Default.aspx/GetBlock?id=${start}`, { headers: { 'Content-Type': 'application/json' } })
        .then(response => response.json())
        .then(data => JSON.parse(data.d));

    const timeList = [];
    for (let i = 0; i <= 4 * 24; i++) {
        const start = DayPilot.Date.today().addMinutes(i * 15);
        const item = {
            name: start.toString("hh:mm tt"),
            id: i * 15
        };
        timeList.push(item);
    }

    const form = [
        {
            type: 'select',
            id: 'startMinutes',
            name: 'Start',
            options: timeList
        },
        {
            type: 'select',
            id: 'endMinutes',
            name: 'End',
            options: timeList
        },
    ];

    const modal = await DayPilot.Modal.form(form, data);

    if (modal.canceled) {
        return;
    }

    fetch("Default.aspx/UpdateBlock",
            {
                headers: {
                    'Content-Type': 'application/json'
                },
                method: "POST",
                body: JSON.stringify(modal.result)
            })
        .then(r => r.json())
        .then(r => {
            const data = JSON.parse(r.d);
            dp.commandCallBack("refresh", { message: data.message });
        });
}
