
import { IFilterMethodType, FilterBase, IValueOfListFilterType } from "./FilterBase";

export type IFilterBooleanJson = boolean | undefined;

export class FilterBoolean extends FilterBase<boolean, boolean, IFilterBooleanJson> {
    getFilter = (): IFilterBooleanJson => {
        if (typeof this.from === 'boolean') {
            return this.from;
        }
        return undefined;
    }


    public setMethodValue = (value: number | undefined) => {
        switch (value) {
            case 0:
                this.method = 'equal';
                break;
            default:
                this.method = undefined;
                break;
        }
    }

    public getMethodValue = () => {
        return this.method === 'from' ? 1 : 0;
    }

    get keyOfMethod() {
        return `${this.key}Method`;
    }
    set keyOfMethod(value: string) { }

    isFiltered = () => {
        if (typeof this.from !== 'undefined' && typeof this.from === 'boolean') {
            return true;
        }

        return false;
    }

    clearFilter = () => {
        this.method = undefined;
        this.from = undefined;
        this.to = undefined;
        this.list = undefined;
        this.multiItemTimer && clearTimeout(this.multiItemTimer);
        this.multiItemTimer = undefined;
        this.multiItem = undefined;
        this.itemOfListValue = undefined;
    }

    public itemOfListValue?: boolean | undefined;

    public multiItem?: IValueOfListFilterType<boolean> | undefined;
    public multiItemTimer?: NodeJS.Timeout;

    constructor(
        public key: string,
        public method: IFilterMethodType,
        public from: boolean | undefined,
        public to: boolean | undefined,
        public list: any
    ) {
        super();
    }

    static empty(key: string, from?: boolean) {
        return new FilterBoolean(key, 'equal', from, undefined, undefined);
    }
}
