import {Component} from '@angular/core';
import {IGridSelectItem} from '../grid-select-item';
import {isNull} from 'util';
import {GridRendererComponent} from '../../grid-renderer-component';
import {GridSelectFactory} from '../grid-select-factory';

@Component({
    selector: 'app-grid-select-renderer',
    templateUrl: './grid-select-renderer.component.html',
    styleUrls: ['./grid-select-renderer.component.scss']
})

export class GridSelectRendererComponent extends GridRendererComponent {

    public itemRange: IGridSelectItem[];
    public currentIndex: number;

    constructor() {
        super();
        this.itemRange = null;
        this.currentIndex = null;
    }

    /**
     * Initialize cell renderer (gets called on cell rendering and after editing / refresh)
     * @param params
     */
    agInit(params: any): void {
        super.agInit(params);
        if (isNull(params.colDef.cellEditorParams.relatesOnDgField)) {
            // Static item range (default behaviour)
            this.setStaticValue(params);
        } else {
            // Item range relates on other dg field
            this.setRelatedValue(params);
        }
    }

    /**
     * Case for static item range (all items in column have the same select items - default)
     * @param params
     */
    private setStaticValue(params: any): void {
        if (this.itemRange == null || this.itemRange == undefined) {
            // Item range is the same for all rows
            this.itemRange = params.colDef.cellEditorParams.values as IGridSelectItem[];
            if (!isNull(params.value)) {
                for (let i = 0; i < this.itemRange.length; i++) {
                    if (this.itemRange[i].key == params.value) {
                        this.value = this.itemRange[i].value;
                        break;
                    }
                }
            } else {
                this.value = params.colDef.cellEditorParams.mandatory ? 'Bitte wählen' : '';
            }
        }
    }

    /**
     * Case for item range related on other dg grid field (Select has always to be updated within init)
     * @param params
     */
    private setRelatedValue(params: any): void {
        // @TODO: Might be checked
        const relatedValue: any = params.colDef.cellEditorParams.relatesOnDgField;
        const itemKey: any = params.colDef.cellEditorParams.itemKey;
        const itemValueKeys: string[] = params.colDef.cellEditorParams.itemValueKeys;

        // Get item range by related value
        this.itemRange = params.data.hasOwnProperty(relatedValue) && !isNull(params.data[relatedValue]) ?
            params.colDef.cellEditorParams.values[params.data[relatedValue]] : null;

        if (!isNull(params.value) && !isNull(this.itemRange)) {
            // let value: string = GridFactory.getGridSelectItemValue()
            for (let i = 0; i < this.itemRange.length; i++) {
                if (this.itemRange[i][itemKey] == params.value) {
                    this.value = GridSelectFactory.getGridSelectItemValue(this.itemRange[i], itemValueKeys);
                    break;
                }
            }
        } else {
            this.value = 'Bitte wählen';
        }
    }

}
