import {create} from "zustand";
import {persist} from "zustand/middleware";
import { removeQuestionsByQuizId } from "./questions";

interface QuizStore {
    listQuizes: { [key: number]: Quizes };
    updateQuizesList: (id: number, values: unknown) => void;
    removeQuiz: (id: number) => void;
    createBlank: () => void;
}

export interface Quizes {
    id: number,
    qid: string,
    deleted: boolean,
    archived: boolean,
    fingerprinting: boolean,
    repeatable: boolean,
    note_prevented: boolean,
    mail_notifications: boolean,
    unique_answers: boolean,
    name: string,
    description: string,
    status: string,
    limit: number,
    due_to: number,
    time_of_passing: number,
    pausable: boolean,
    version: number,
    version_comment: string,
    created_at: string,
    updated_at: string,
    question_cnt: number,
    passed_count: number,
    average_time: number,
    super: true,
    group_id: number,
    step: number,
    startpage: string,
    createResult: boolean,
    config:  {
        type: string,
        logo: string,
        noStartPage: boolean,
        startpage: {
            description: string,
            button: string,
            position: string,
            background: {
                type: string,
                desktop: string,
                mobile: string,
                video: string,
                cycle: boolean
            },
        },
        info: {
            phonenumber: string,
            clickable: boolean,
            orgname: string,
            site: string,
            law?: string
        },
        meta: string,
    }
}

/** @deprecated */
export const quizStore = create<QuizStore>()(
    persist(
        (set, get) => ({
            listQuizes: {
            },
            updateQuizesList: (id: number, values: any) => {
                const state = get()["listQuizes"] || {};
                state[id] = {
                    ...state[id],
                    ...values
                };
                set({listQuizes: state});
            },
            removeQuiz: (id) => {
                const state = get()["listQuizes"] || {};

                const newState = Object.entries(state).reduce<any>((accumulator, [key, value], index, array) => {
                    if (key !== id.toString()) {
                        accumulator[key] = value;
                    }
                    return accumulator;
                }, {});
                set({listQuizes: newState});

                removeQuestionsByQuizId(id);
            },
            createBlank: () => {
                const id = getRandom(1000000, 10000000)
                const newListQuizes = get()["listQuizes"] || {};
                newListQuizes[id] = {
                    "id": id,
                    "qid": "",
                    "deleted": false,
                    "archived": true,
                    "fingerprinting": true,
                    "repeatable": true,
                    "note_prevented": true,
                    "mail_notifications": true,
                    "unique_answers": true,
                    "name": "",
                    "description": "",
                    "status": "",
                    "limit": 0,
                    "due_to": 0,
                    "time_of_passing": 0,
                    "pausable": true,
                    "version": 0,
                    "version_comment": "",
                    "created_at": "",
                    "updated_at": "",
                    "question_cnt": 0,
                    "passed_count": 0,
                    "average_time": 0,
                    "super": true,
                    "group_id": 0,
                    "step": 1,
                    "startpage": "",
                    "createResult": false,
                    "config": {
                        "noStartPage": false,
                        "type": "", // quiz или form
                        "logo": "hub.pena.digital/img/logo",
                        "startpage": {
                            "description": "",// приветственный текст опроса
                            "button": "", // текст на кнопке начала опроса
                            "position": "ltr", // ltr или rtl. отображение элементов поверх фона
                            "background": {
                                "type": "image", //image или video
                                "desktop": "",
                                "mobile": "",
                                "video":"",
                                "cycle": true //зацикливать видео или нет
                            },
                        },
                        "info": {
                            "phonenumber": "",
                            "clickable": true,
                            "orgname": "",
                            "site": "",
                            "law": ""
                        },
                        "meta": "что-то"
                    }
                };
                set({listQuizes: newListQuizes});
                return id;
            },
        }),
        {
            name: "quizes",
        }
    )
);

function getRandom(min: number, max: number) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min)) + min;
}
