/**
 * Created by rburson on 9/1/17.
 */
import "babel-polyfill";
import test from "blue-tape";
import { Catavolt, propertyFormatter } from "../../catavolt/dialog/Catavolt";
import { SearchDialog } from "../../catavolt/models";
import { RedirectionUtil } from "../../catavolt/models/RedirectionUtil";
import { TypeNames } from "../../catavolt/models/types";
import { Log } from "../../catavolt/util/Log";
import { LogLevel } from "../../catavolt/util/Log";
/*
    Get a reference to the SDK instance
 */
/* tslint:disable */
let [tenantId, userId, password, sessionId, workbenchId, workbenchLaunchId] = ['cvtutorial', 'wsmith', 'biznes1', null, 'AAABACffAAAAAE8X', 'AAABACfaAAAAAKE8'];
const currentWorkbenches = null;
let currentRedirection = null;
let currentDialog = null;
Log.logLevel(LogLevel.DEBUG);
test("Invalid Login Test", (t) => {
    return t.shouldFail(Catavolt.login(tenantId, 'DESKTOP', userId, 'not a valid password')
        .catch(error => {
        //show the error we are expecting
        t.comment(`> ${JSON.stringify(error)}`);
        throw error;
    }));
});
test("Login Test", (t) => {
    return Catavolt.login(tenantId, 'DESKTOP', userId, password).then((session) => {
        t.ok(session, 'Expecting a non-null result');
        t.ok(session.id, 'Expecting a session id');
        t.comment(`> SessionId: ${session.id}`);
        sessionId = session.id;
        return session;
    });
});
test("Launch Workbench By id Test", (t) => {
    return Catavolt.performWorkbenchActionForId(workbenchId, workbenchLaunchId)
        .then((successOrRedirection) => {
        t.ok(successOrRedirection, 'Expecting a non-null result');
        t.ok(RedirectionUtil.isDialogRedirection(successOrRedirection), 'Expecting a DialogRedirection type');
        currentRedirection = successOrRedirection;
        t.comment(`> DialogRedirection: {id: ${currentRedirection.id}, description: ${currentRedirection.dialogDescription}}`);
        return successOrRedirection;
    });
});
test("Open A DialogRedirection Test", (t) => {
    return Catavolt.openDialog(currentRedirection).then((dialog) => {
        t.ok(dialog, 'Expecting a non-null result');
        t.ok(dialog.id, 'Expecting a dialog id');
        t.ok(dialog.type === TypeNames.EditorDialogTypeName, 'Expecting an EditorDialog');
        t.ok(dialog.view.type === TypeNames.FormTypeName, 'Expecting a top-level Form');
        t.comment(`> Dialog: {id: ${dialog.id}, view: {type: ${dialog.view.type}}}`);
        const childObj = dialog.children.map((child) => {
            return { id: child.id, view: { type: child.view.type } };
        });
        t.comment(`>   Dialog Children: ${JSON.stringify(childObj)}`);
        currentDialog = dialog;
        return dialog;
    });
});
test("Load And Page A List Test", (t) => {
    const queryDialog = currentDialog.children[0];
    const list = queryDialog.view;
    t.comment(`List: ${queryDialog.description}`);
    t.comment(`>   List Columns: ${list.columnHeadings.join(',')}`);
    queryDialog.initScroller(5);
    //scroll forward with the specified number of records
    return queryDialog.scroller.refresh().then((records) => {
        const rows = records.map((record) => {
            return record.properties.map((property) => {
                return propertyFormatter.formatForRead(property, queryDialog.recordDef.propDefAtName(property.name));
            }).join(',');
        });
        t.comment(`>   First 5 Records (formatted for 'read mode'): `);
        rows.forEach(row => t.comment(`>      ${row}`));
        //scroll forward with a specific number of records (override)
        return queryDialog.scroller.pageForward(20).then((records) => {
            const rows = records.map((record) => {
                return record.properties.map((property) => {
                    return propertyFormatter.formatForWrite(property, queryDialog.recordDef.propDefAtName(property.name));
                }).join(',');
            });
            t.comment(`>   Next 20 Records (formatted for 'write mode'):`);
            rows.forEach(row => t.comment(`>      ${row}`));
            //scroll forward with the previously specified default number of records
            return queryDialog.scroller.pageForward().then((records) => {
                const rows = records.map((record) => record.propValues.join(','));
                t.comment(`>   Next 5 Records:`);
                rows.forEach(row => t.comment(`>      ${row}`));
                return records;
            });
        });
    });
});
test("Open Search For A List", (t) => {
    const queryDialog = currentDialog.children[0];
    const list = queryDialog.view;
    t.comment(`Open search for List: ${queryDialog.description}`);
    return queryDialog.openSearch().then((redirection) => {
        Catavolt.openDialog(redirection).then((dialog) => {
            t.ok(dialog instanceof SearchDialog);
            return dialog;
        });
    });
});
test("Perform Action Test", (t) => {
    const queryDialog = currentDialog.children[0];
    const list = queryDialog.view;
    //Get the 'default action' on this QueryDialog
    const defaultActionId = queryDialog.defaultActionId;
    const menu = list.menu.findAtActionId(defaultActionId);
    t.ok(menu, "The View's Menu should contain the defaultActionId");
    //Choose a record to 'act on'
    const aRecord = queryDialog.scroller.buffer[2];
    t.comment(`Navigating to actionId: ${defaultActionId}`);
    //Use toDialogOrRedirection Higher-Order Function to automatically open
    //the Redirection returned by performMenuAction
    return Catavolt.toDialogOrRedirection(
    // Perform the action
    queryDialog.performMenuAction(menu, [aRecord.id])
        .then((successOrRedirection) => {
        if (RedirectionUtil.isDialogRedirection(successOrRedirection)) {
            currentRedirection = successOrRedirection;
        }
        return successOrRedirection;
    })).then((dialogOrRedirection) => {
        if (!RedirectionUtil.isRedirection(dialogOrRedirection)) {
            const dialog = dialogOrRedirection;
            t.ok(dialog, 'Expecting a non-null result');
            t.ok(dialog.id, 'Expecting a dialog id');
            t.ok(dialog.type === TypeNames.EditorDialogTypeName, 'Expecting an EditorDialog');
            t.ok(dialog.view.type === TypeNames.FormTypeName, 'Expecting a top-level Form');
            t.comment(`> Dialog: {id: ${dialog.id}, view: {type: ${dialog.view.type}}}`);
            const childObj = dialog.children.map((child) => {
                return { id: child.id, view: { type: child.view.type } };
            });
            t.comment(`>   Dialog Children: ${JSON.stringify(childObj)}`);
            currentDialog = dialog;
        }
        return dialogOrRedirection;
    });
});
test("Read A Record From An EditorDialog Test", (t) => {
    const editorDialog = currentDialog.children[0];
    const details = editorDialog.view;
    t.comment(`Detail: ${editorDialog.description}`);
    // Get the Cell Layout defined for this Details Object
    const rowsLayout = details.rows.map((row) => {
        return row.map((cell) => {
            return cell.values.map((cellValue) => {
                return `[${cellValue.type}]`;
            }).join(', ');
        }).join(', ');
    });
    t.comment(`>  Layout is: `);
    rowsLayout.forEach(row => t.comment(`>    ${row}`));
    // Read the Record data
    return editorDialog.read().then((record) => {
        t.ok(record);
        t.comment(`>  Record is: ${record.propValues.join(', ')}`);
        return record;
    });
});
test("Read A Binary Property From EditorDialog Test", (t) => {
    const editorDialog = currentDialog.children[0];
    const details = editorDialog.view;
    return editorDialog.read().then((record) => {
        const largePropDefs = editorDialog.recordDef.propertyDefs
            .filter((propDef) => propDef.isLargePropertyType);
        const loadPrArray = largePropDefs.map((propDef) => {
            return editorDialog.readLargeProperty(propDef.propertyName, record.id);
        });
        return Promise.all(loadPrArray).then((largeProperties) => {
            t.ok(largeProperties);
            t.comment(`    Read ${largeProperties.length} LargeProperties`);
            largeProperties.forEach((largeProperty) => {
                t.ok(largeProperty.encodedData);
                // t.comment(`>    ${largeProperty.toUrl()}`);
            });
            return largeProperties;
        });
    });
});
/*
test("Read A Url as a Stream", (t) => {
    return Catavolt.openStream('https://dialog.hxgn-api.net/v0/openapi.yaml').then((readableClientResponse) => {
       t.ok(readableClientResponse);
       t.comment(`> Opened URL Stream for ${readableClientResponse}`);
       readableClientResponse.read().then((result: { done: boolean, value: any }) => {
           t.comment(`>    Chunk: ${result.value}`);
       });
    });
});
*/
test("Logout Test", (t) => {
    return Catavolt.logout().then((response) => {
        t.ok(response);
        t.ok(response.sessionId);
        t.equal(sessionId, response.sessionId);
        t.comment(`> Session Logout: ${response.sessionId}`);
    });
});
