import { ColumnItem } from "../ColumnItem.class";
import { FilterDTO } from "./FilterDTO.class";

export class Filter extends ColumnItem {
    constructor(){
        super();
        this.columnName = ''
        this.filter = ''
        this.low_value = ''
        this.higth_value = ''
    }
    columnName: string;
    filter: string;
    low_value: string;
    higth_value: string

    baseFilter(){
        let filterDto: FilterDTO;
        filterDto = new FilterDTO();
        filterDto.col_name = this.columnName;
        filterDto.col_expr = this.filter;
        filterDto.col_value = "";

        return filterDto;
    }
    defaultFilter(){
        console.log('defaultFilter');
        let filterDto: FilterDTO;
        filterDto = this.baseFilter();
        if(this.datatype == 'TEXT' || this.datatype == 'DATE')filterDto.col_value = "'" + this.value + "'";
        else if(this.datatype == 'NUMBER')filterDto.col_value = this.value;

        return filterDto;
    }

    nullFilter(){
        console.log('nullFilter');
        let filterDto: FilterDTO;
        filterDto = this.baseFilter();
        filterDto.col_value = (filterDto.col_expr == 'IS NULL') ? "NULL" : "NOT NULL";
        filterDto.col_expr = 'IS' ;

        return filterDto;
    }

    likeFilter(){
        console.log('likeFilter');
        let filterDto: FilterDTO;
        filterDto = this.baseFilter();
        if(this.datatype === 'TEXT')filterDto.col_value = "'%" + this.value + "%'";

        return filterDto;
    }

    betweenFilter(){
        let filterDto: FilterDTO;
        filterDto = this.baseFilter();
        if(this.datatype === 'TEXT' || this.datatype === 'DATE')filterDto.col_value = "'" + this.low_value + "'" + " AND " + "'" + this.higth_value + "'";
        else if(this.datatype === 'NUMBER')filterDto.col_value = this.low_value + " AND " + this.higth_value;

        return filterDto;
    }

    startFilter(){
        let filterDto: FilterDTO;
        filterDto = this.baseFilter();
        filterDto.col_expr = (this.filter === 'START')? "LIKE" : "NOT LIKE";
        if(this.datatype === 'TEXT')filterDto.col_value = "'" + this.value + "%'";

        return filterDto;
    }

    toFilterDTO(){
        let filterDto: FilterDTO;

        switch(this.filter){
            case 'IS NULL': filterDto = this.nullFilter(); break;
            case 'IS NOT NULL': filterDto = this.nullFilter(); break;
            case 'BETWEEN': filterDto = this.betweenFilter(); break;
            case 'NOT BETWEEN': filterDto = this.betweenFilter(); break;
            case 'LIKE': filterDto = this.likeFilter(); break;
            case 'NOT LIKE': filterDto = this.likeFilter(); break;
            case 'START': filterDto = this.startFilter(); break;
            case 'NOT START': filterDto = this.startFilter(); break;
            default:  filterDto = this.defaultFilter(); break;
        }
        return filterDto;
    }

    parseToFilter(filters: any){
        let filterparsed : Filter[];
        filterparsed = [];
        for(let index=0; filters.length < index; index++){
            let filter: Filter;
            filter = new Filter();
            filter.filter = filters[index].col_expr;
            filter.value = filters[index].col_value;
            filter.columnName = filters[index].col_name;
            filter.higth_value = '';
            filter.low_value = '';
            filterparsed.push(filter);
        }

        return filterparsed;
    }
}