
import { Base } from './../';

import { MenuRoles, Menus, Pages, Models } from './../';


class Modules extends Base {

    constructor() {
        super();

        this.fields = [
            {
                field: 'name',
                caption: 'Name',
            },

            {
                field: 'name',
                caption: 'Name',
            },

            {
                field: 'display_order',
                caption: 'Display Order',
            },
        ];

        // this.columns = ;
    }

    get getEndpoint() {
        return 'modules';
    }

    get path() {
        return `modules`;
    }

    get getName() {
        return this.modelName + `s`;
    }

    get modelName() {
        return `module`;
    }

    loadMenus = () => {
        return this.get().then((result) => {
            // console.log(result);

            // return result;
            return Promise.all(result.modules.map(this.loadDetail)).then((result) => {
                // console.log(result);

                return result;
            });
        });
    };

    /**
     * Load all the related records of the module
     * This should ideally be dependendent on the relationship of this model
     * to pull out data in a generic way
     *
     * @param {*} item
     * @returns
     */
    loadDetail = (item) => {
        var queries = [
            {
                field: 'module_id',
                value: item.id,
            },
        ];

        // load the menus in the module
        return Menus.get(queries)
            .then((result) => {

                console.log(result);

                return {
                    ...item,
                    caption: item.name,
                    menus: result.menus,
                };
            })
            .then((result) => {
                var menus = [];

                // For each menu we check if the user has access
                return Promise.all(
                    result.menus.map((menu) => {
                        return this.validateAccess(menu).then((response) => {
                            if (response) {
                                menus.push(response);
                            }

                            return response;
                        });
                    })
                ).then(() => {
                    // console.log(menus);

                    return {
                        ...result,
                        menus: menus,
                    };
                });
            })
            .then((result) => {
                return result;

                // For Each Menu get the detail
                // return Promise.all(result.menus.map(this.loadPageDetail)).then((response) => {
                //     // console.log(response);

                //     return {
                //         ...result,
                //         menus: response,
                //     };
                // });
            }).catch((error) => {

                console.log(error);
            })
        // .then((final) => {
        //     console.log(final);

        //     return {
        //         ...final,
        //     };
        // });
    };

    /**
     * Validate access for the user
     */
    validateAccess = (menu) => {

        // Getting the user object
        const user = this.getUser();

        // return new Promise((resolve) => {
        return MenuRoles.get([
            {
                field: 'menu_id',
                value: menu.id,
            },
            {
                field: 'role_id',
                value: user.role.id,
            },
        ]).then((result) => {
            // console.log(result);

            if (result.menu_roles.length) {
                return menu;
            } else {
                return false;
            }
        });
        // });
    };

    /**
     * Load the page detail of the menu
     *
     * @param {*} menu
     * @returns
     */
    loadPageDetail = (menu) => {
        // Get the page record
        return Pages.getRecord(menu.page_id).then((response) => {
            // Get the model record
            return Models.getRecord(menu.model_id).then((model_response) => {
                // Combine to form the total data
                return {
                    ...menu,
                    caption: menu.name,
                    page: response,
                    model: model_response
                };
            });
        });
    };

    get columns() {
        return [
            {
                field: 'name',
                caption: 'Name',
            },
            {
                field: 'display_order',
                caption: 'Display Order',
            },
        ];
    }

}

export default Modules;
