import * as React from "react";
import { FieldsControl as CoreFieldsControl } from "../../../Controls/FieldsControl";
import { RecordField, ListRecordsCriteria, FieldQuery, Pagination } from "../../../Models";
import { RecordService } from "../../../Services";
import { LoadingService } from "../../../Services/LoadingService";

export class FieldsControl extends React.Component<any, any> {
    private _recordService: RecordService = new RecordService();
    private _loadingService = new LoadingService();
    constructor(props: any) {
        super(props);

    }

    private _onConfientialityChange(value: number) {

        let confidentialUsersField: RecordField = new RecordField();
        this.props.fields.forEach((field: RecordField) => {
            if (field.id == 200)
                confidentialUsersField = field;
        });

        if (confidentialUsersField.id) {
            if (value == 61) {
                confidentialUsersField.hidden = false;
                confidentialUsersField.allowNull = false;
            }
            else {
                confidentialUsersField.hidden = true;
                confidentialUsersField.allowNull = true;
            }
        }

        this.forceUpdate();
    }

    private _onTargetChange(value: number) {
        let contactField: RecordField = new RecordField();
        this.props.fields.forEach((field: RecordField) => {

            if (field.id == 202)
                contactField = field;
        });

        if (contactField.id) {
            if (value == 19) {
                contactField.hidden = false;
                contactField.allowNull = false;
            }
            else {
                contactField.hidden = true;
                contactField.allowNull = true;
            }
        }

        this.forceUpdate();
    }

    private _loadContacts(field: RecordField) {
        let criteria: ListRecordsCriteria = new ListRecordsCriteria();
        criteria.asLookup = false;
        criteria.entityId = field.extraData;
        criteria.query = new Array<FieldQuery>();
        criteria.pagination = new Pagination();
        criteria.pagination.start = 0;
        criteria.pagination.count = 99999;
        this._loadingService.show();
        this._recordService.listRecords(criteria).then(result => {
            field.records = result;
            this.forceUpdate();
            this._loadingService.hide();

        }, error => {
            alert(error);
            this._loadingService.hide();

        });
    }

    private _setColWidth(id: any): string {
        switch (id) {
          case 157: {
            return "ms-Grid-col ms-sm12 ms-lg12";
          }
          case 155: {
            return " ms-Grid-col ms-sm12 ms-lg6";
          }
          case 159: {
            return "ms-Grid-col ms-sm12 ms-lg6";
          }
          case 163: {
            return "ms-Grid-col ms-sm12 ms-lg6";
          }
          case 164: {
            return "ms-Grid-col ms-sm12 ms-lg12";
          }
          case 165: {
            return "ms-Grid-col ms-sm12 ms-lg12";
          }
          case 189: {
            return "ms-Grid-col ms-sm12 ms-lg6";
          }
          case 202: {
            return "ms-Grid-col ms-sm12 ms-lg12";
          }
          case 256: {
            return "ms-Grid-col ms-sm12 ms-lg6";
          }
          case 257: {
            return "ms-Grid-col ms-sm12 ms-lg6";
          }
          case 258: {
            return "ms-Grid-col ms-sm12 ms-lg12";
          }
    
          default: {
            return "ms-Grid-col ms-sm12 ms-lg12";
          }
        }
      }

    public render(): JSX.Element {

        if (this.props.fields)
            this.props.fields.forEach((field: RecordField) => {
                field.onChanged = (field: RecordField) => {
                    if (field.id == 189) {
                        this._onConfientialityChange(field.data);
                    }

                    if (field.id == 164) {
                        this._onTargetChange(field.data);
                    }
                }

                if (field.id == 202 && field.records == null) {
                    this._loadContacts(field);
                }

                if (field.id == 170 || field.id == 174 || field.id == 175 || field.id == 176 || field.id == 156 || field.id == 181 || field.id == 182
                    || field.id == 190 || field.id == 191 || field.id == 192 || field.id == 194 || field.id == 199 || field.id == 201
                    || field.id == 204 || field.id == 165)
                    field.hidden = true;
               
            });

        return (
            <div><CoreFieldsControl
                {...this.props} fields={this.props.fields} 
                setColWidth={this._setColWidth.bind(this)}></CoreFieldsControl>
            </div>
        )
    }
}                        