﻿
import { TfabricaCrudField } from './tfabrica.crud.field.model';
import { TfabricaCrudFilter } from './tfabrica.crud.filter.model';
import { TfabricaCrudFilterValue } from './tfabrica.crud.filter.value';
import 'rxjs/Rx';
import { Observable, BehaviorSubject } from "rxjs";

export class TfabricaCrudReport
{

    public title: string;
    public titleSubject = new BehaviorSubject(""); 

    public executeImmediately: boolean;

    public connectionString: string;
    public selectRead: string;
    public updateTableName: string;

    public composedCommand: string;

    public readedRows: any[];
    public updateRow: any;

    public fields: TfabricaCrudField[];
    public fieldsToDisplay: TfabricaCrudField[];

    public filters: TfabricaCrudFilter[];

    public success: boolean;
    public message: string;

    //public canShow: boolean;
    public canAdd: boolean;
    public canRemove: boolean;
    public canUpdate: boolean;
    public canSearch: boolean;
   //public canFilter: boolean;
    public canManageCustom: boolean;
    public customFunction: string;

    constructor()
    {
        this.readedRows = new Array();
        this.fields = new Array();
    }

    public setFromJson(json)
    {
        this.connectionString = json.connectionString;
        //this.fields = json.fields;
        this.setFieldsFromJson(json.fields);
        if (this.fields == undefined) this.fields = new Array();
        this.readedRows = json.readedRows;
        this.setFieldsToDisplay();
        this.message = json.message;
        this.success = json.success;
        this.title = json.title;
        this.titleSubject.next(this.title);
        this.executeImmediately = json.executeImmediately;

        this.selectRead = json.selectRead;
        this.updateTableName = json.updateTableName;

        this.setFields(this.fields);
        this.filters = json.filters;

        this.composedCommand = json.composedCommand;

//        this.canShow = json.canShow;
        this.canAdd  = json.canAdd;
        this.canRemove = json.canRemove;
        this.canUpdate = json.canUpdate;
        this.canSearch = json.canSearch;
        //this.canFilter = json.canFilter;
        this.canManageCustom = json.canManageCustom;
        this.customFunction = json.customFunciton;
        
    }

    public setFieldsFromJson(jsonFields)
    {
        //console.log("setFieldsFromJson");
        //console.log(jsonFields);

        this.fields = new Array();
        let that = this;

        try {
            jsonFields.forEach(function (entry) {
                let field = new TfabricaCrudField();
                field.name = entry.name;
                field.label = entry.label;
                field.type = entry.type;
                field.display = entry.display;
                field.conversionFunction = entry.conversionFunction;
                field.isUpdate = entry.isUpdate;
                field.isKey = entry.isKey;
                field.style = entry.style;
                that.fields.push(field);
            });
        } catch (Err) { }
    }

    public setFieldsToDisplay()
    {
        let that = this;
        this.fieldsToDisplay = new Array();

        let position = 0;

        try {

            this.fields.forEach(function (entry: TfabricaCrudField)
            {
                position++;

                if (entry.display) {
                    if (entry.position == 0 || entry.position == undefined) entry.position = position;
                    //console.log("setFieldsToDisplay()");
                    //console.log(entry);
                    that.fieldsToDisplay.push(entry);
                }
            });
            this.fieldsToDisplay.sort(TfabricaCrudField.compare);
        } catch (Err) { }
    }

    public setFields(fieldsInput)
    {
        console.log("setFields:");
        console.log(fieldsInput);
        this.fields = new Array();
        let that = this;
        try {
            fieldsInput.forEach(function (entry)
            {
                let field = new TfabricaCrudField();
                field.name = entry.name;
                field.label = entry.label;
                field.type = entry.type;
                field.display = entry.display;
                field.conversionFunction = entry.conversionFunction;
                field.isUpdate = entry.isUpdate;
                field.isKey = entry.isKey;
                field.style = entry.style;
                field.position = entry.position;
                that.fields.push(field);
            });

            //this.fields = fields;
            this.fields.sort(TfabricaCrudField.compare);
            this.setFieldsToDisplay();
            return this.fields;
        } catch (Err) { }
    }

    public getFields()
    {
        this.fields.sort(TfabricaCrudField.compare);
        return this.fields;
    }

    public getFieldsToDisplay()
    {
        try {
            this.fieldsToDisplay.sort(TfabricaCrudField.compare);
        } catch (Err) { }
        return this.fieldsToDisplay;
    }

    public setFiltersValuesFromUserParameters(parameters) {

        let that = this;
        try {
            that.filters.forEach(function (filter) {
                if (filter.userParameterField != undefined && filter.userParameterField != "") {
                    if (filter.values == undefined) filter.values = new Array();
                    parameters.forEach(function (userParameter) {
                        if (userParameter.key == filter.userParameterField) {
                            userParameter.values.forEach(function (userParameterValue) {
                                let actValue = new TfabricaCrudFilterValue();
                                actValue.value = userParameterValue.value;
                                actValue.label = userParameterValue.label;
                                actValue.isSelected = false;
                                filter.values.push(actValue);
                            });
                        }
                    });
                }
            });
        } catch (Err) { }

    }

}