import { observable } from "mobx";
import { Column, GridType } from "../types";
import { GridModel } from "./GridModel";

export enum ColumnEvent {
    DescriptionChanged,
    TitleChanged,
    ReadOnlyChanged,
    TypeChanged,
    VisibleChanged,
    GroupSelectChanged,
    SuffixChanged
}

export class ColumnModel {

    title : string;

    @observable
    width : string;

    @observable
    selected : boolean;

    @observable
    order : number;

    @observable
    readOnly : boolean;

    @observable
    visible : boolean;

    property : string;

    description : string;

    @observable
    groupSelect : boolean;

    @observable
    type : GridType;

    grid : GridModel;

    constructor(column : Column, grid : GridModel) {
        this.title = column.Title;
        this.property = column.Property;
        this.width = column.Width || "100px";
        this.selected = false;
        this.readOnly = column.ReadOnly;
        this.visible = column.Visible;
        this.description = column.Description;
        this.grid = grid;
        this.groupSelect = column.GroupSelect !== false;

        if (typeof column.Type === "string") {
            this.type = {
                kind: column.Type
            };
        }
        else if (column.Type) {
            this.type = column.Type;
        }
        else {
            this.type = {
                kind: "string"
            };
        }

        this.setSelected = this.setSelected.bind(this);
    }

    setSelected() {
        this.selected = true;
    }

    setType(newType, noEvent? : boolean) {
        if (typeof newType === "string") {
            this.type = {
                kind: newType
            };
        }
        else {
            this.type = newType;
        }
    }

    serialize() {
        return {
            readOnly: this.readOnly,
            title: this.title,
            property: this.property,
            visible: this.visible,
            description: this.description,
            type: this.type,
            groupSelect: this.groupSelect
        }
    }

    handleEvent(event : ColumnEvent, args : any[]) {

        // console.log(`ColumnEvent[${ColumnEvent[event]}]{${this.property} => ${JSON.stringify(args)}`);

        switch (event) {
            case ColumnEvent.DescriptionChanged:
                this.description = args[0];
                break;
            case ColumnEvent.TitleChanged:
                this.title = args[0];
                break;
            case ColumnEvent.ReadOnlyChanged:
                this.readOnly = args[0];
                break;
            case ColumnEvent.TypeChanged:
                this.setType(args[0], true);
                break;
            case ColumnEvent.VisibleChanged:
                this.visible = args[0];
                break;
            case ColumnEvent.GroupSelectChanged:
                this.groupSelect = args[0];
                break;
            case ColumnEvent.SuffixChanged:
                this.type.suffix = args[0];
                break;
            case ColumnEvent.GroupSelectChanged:
                this.groupSelect = args[0];
                break;
            case ColumnEvent.SuffixChanged:
                this.type.suffix = args[0];
                break;
            default:
                break;
        }
    }
}
