import {
    Component,
    Input,
    AfterViewInit,
    Output,
    EventEmitter,
    OnChanges,
    SimpleChange,
    OnInit
} from '@angular/core';
import { AppConstants } from '../shared/app.constants';
import { WorkspaceService } from '../workspace';
import { FrameworkLibraryService } from 'angular2-json-schema-form';
import { Router, ActivatedRoute } from '@angular/router';


@Component({ selector: 'template-designer', templateUrl: './template-designer.component.html' })

export class TemplateDesignerComponent {
    private jsonFormSchema: any;
    private formIsValid: boolean = null;
    private jsonFormValid: boolean = false;
    private jsonFormObject: any;

    constructor(private workspaceService: WorkspaceService, private frameworkLibrary: FrameworkLibraryService, private route: ActivatedRoute, private router: Router) {
        this.generateDataForm();
    }

    generateDataForm() {
        this.jsonFormSchema = {
            "schema": {
                "type": "object",
                "title": "Types",
                "properties": {
                    "docDscr": {
                        "type": "object",
                        "description": "Study Description",
                        "properties": {
                            "depositr": {
                                "type": "array",
                                "description": "Depositor",
                                "items": {
                                    "type": "object",
                                    "properties": {
                                        "name": {
                                            "title": "Name",
                                            "type": "string"
                                        },
                                        "abbr": {
                                            "title": "Abbreviation",
                                            "type": "string"
                                        },
                                        "affiliation": {
                                            "title": "Affiliation",
                                            "type": "string"
                                        }
                                    }
                                },
                                "required": [
                                    "name"
                                ]
                            },
                            "prodDate": {
                                "title": "Date of Production",
                                "type": "string"
                            },
                            "documentVersion": {
                                "type": "string",
                                "title": "DDI Document Version",
                                "description": "Documenting a dataset is not a trivial exercise. Producing metadata is probably impossible."
                            },
                            "documentIDNumber": {
                                "type": "string",
                                "title": "DDI Document ID Number"
                            },
                            "dataCollector": {
                                "title": "Data collector",
                                "type": "array",
                                "items": {
                                    "type": "object",
                                    "properties": {
                                        "name": {
                                            "title": "Name",
                                            "type": "string"
                                        },
                                        "abbr": {
                                            "title": "Abbreviation",
                                            "type": "string"
                                        },
                                        "affiliation": {
                                            "title": "Affiliation",
                                            "type": "string"
                                        }
                                    }
                                }
                            },
                            "producer": {
                                "type": "array",
                                "description": "Producers",
                                "items": {
                                    "type": "object",
                                    "properties": {
                                        "name": {
                                            "title": "Name",
                                            "type": "string"
                                        },
                                        "abbr": {
                                            "title": "Abbreviation",
                                            "type": "string"
                                        },
                                        "role": {
                                            "title": "Role",
                                            "type": "string"
                                        },
                                        "affiliation": {
                                            "type": "string",
                                            "title": "Affiliation"
                                        }
                                    }
                                },
                                "required": [
                                    "name"
                                ]
                            }
                        }
                    }
                }
            },
            "form": [
                {
                    "key": "docDscr",
                    "type": "fieldset",
                    "title": "Document Description",
                    "expandable": true,
                    "items": [
                        {
                            "key": "MetadataPreparation",
                            "type": "fieldset",
                            "title": "Metadata Preparation",
                            "isVirtual": true,
                            "expandable": true,
                            "items": [
                                {
                                    "key": "docDscr.producer",
                                    "type": "table",
                                    "htmlClass": "table table-bordered",
                                    "title": "Metdata Producers",
                                    "expandable": false,
                                    "items": [
                                        {
                                            "key": "docDscr.producer[].name",
                                            "title": "Name",
                                            "htmlClass": "col-xs-3",
                                            "fieldHtmlClass": "form-control"
                                        },
                                        {
                                            "key": "docDscr.producer[].abbr",
                                            "title": "Abbreviation",
                                            "htmlClass": "col-xs-3",
                                            "fieldHtmlClass": "form-control"
                                        },
                                        {
                                            "key": "docDscr.producer[].role",
                                            "title": "Role",
                                            "htmlClass": "col-xs-3",
                                            "fieldHtmlClass": "form-control"
                                        },
                                        {
                                            "key": "docDscr.producer[].affiliation",
                                            "title": "Affiliation",
                                            "htmlClass": "col-xs-3",
                                            "fieldHtmlClass": "form-control"
                                        }
                                    ]
                                },
                                {
                                    "key": "docDscr.depositr",
                                    "type": "table",
                                    "htmlClass": "table table-bordered",
                                    "title": "DDI Depositors",
                                    "expandable": false,
                                    "items": [
                                        {
                                            "key": "docDscr.depositr[].name",
                                            "title": "Name",
                                            "type": "text",
                                            "htmlClass": "col-xs-4",
                                            "fieldHtmlClass": "form-control"
                                        },
                                        {
                                            "key": "docDscr.depositr[].abbr",
                                            "title": "Abbreviation",
                                            "type": "text",
                                            "htmlClass": "col-xs-4",
                                            "fieldHtmlClass": "form-control"
                                        },
                                        {
                                            "key": "docDscr.depositr[].affiliation",
                                            "title": "Affiliation",
                                            "type": "text",
                                            "htmlClass": "col-xs-4",
                                            "fieldHtmlClass": "form-control"
                                        }
                                    ]
                                },
                                {
                                    "key": "docDscr.prodDate",
                                    "type": "date",
                                    "title": "Date of Production"
                                },
                                {
                                    "key": "docDscr.documentVersion",
                                    "type": "textarea",
                                    "title": "DDI Document Version"
                                },
                                {
                                    "key": "docDscr.documentIDNumber",
                                    "type": "text",
                                    "title": "DDI Document ID Number"
                                },
                                {
                                    "key": "docDscr.dataCollector",
                                    "type": "table",
                                    "htmlClass": "table table-bordered",
                                    "title": "Collection(s)",
                                    "expandable": false,
                                    "items": [
                                        {
                                            "key": "docDscr.dataCollector[].name",
                                            "title": "Name",
                                            "type": "text",
                                            "htmlClass": "col-xs-4",
                                            "fieldHtmlClass": "form-control"
                                        },
                                        {
                                            "key": "docDscr.dataCollector[].affiliation",
                                            "title": "Affiliation",
                                            "type": "text",
                                            "htmlClass": "col-xs-4",
                                            "fieldHtmlClass": "form-control"
                                        },
                                        {
                                            "key": "docDscr.dataCollector[].role",
                                            "title": "Role",
                                            "type": "text",
                                            "htmlClass": "col-xs-4",
                                            "fieldHtmlClass": "form-control"
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ],
            "value": {
                "docDscr": {
                    "documentVersion": "Nithya",
                    "documentIDNumber": "100"
                }
            }
        };
        this.generateForm(this.jsonFormSchema);
    }

    private generateForm(newFormString: any) {
        if (!newFormString) { return; }

        // Most examples should be written in pure JSON, but if a schema includes
        // a function, the playground will compile it as Javascript instead
        try {
            // Parse entered content as JSON
            this.jsonFormObject = newFormString;
            this.jsonFormValid = true;
        } catch (jsonError) {

        }
    }

    public yourOnChangesFn(event) {
        alert(JSON.stringify(event));
    }
}