import * as React from "react";
import { Column, Option, SelectOption } from "../types";

function parseFloatOrUndefined(value : any) {
    const number = parseFloat(value);
    if (!number && number !== 0) {
        return undefined;
    }
    return number;
}

export function getColumnsFromChildren(children : any) : Column[] {
    let columns : Column[] = [];

    React.Children.forEach(children, (child : React.ReactChild, index : number) => {
        if (typeof child !== "object") {
            return;
        }

        if (!(child && child.type)) {
            return;
        }

        const childType : any = child.type;

        let column : Column = {
            Property: child.props.property,
            Title: child.props.title || child.props.property,
            Description: child.props.description,
            Visible: child.props.visible !== false,
            ReadOnly: child.props.readOnly === true || (child.props.readOnly + "").toLowerCase() === "true",
            GroupSelect: (child.props.groupSelect !== "false") && child.props.groupSelect !== false,
            Width: child.props.width
        } as any;

        switch (childType.name || childType.displayName) {
            case "Number":
            case "NumberColumn":
                column.Type = {
                    kind: "number",
                    min: parseFloatOrUndefined(child.props.min),
                    max: parseFloatOrUndefined(child.props.max),
                    step: parseFloatOrUndefined(child.props.step),
                    decimals: parseFloatOrUndefined(child.props.decimals),
                    suffix : child.props.suffix
                };
                break;
            case "String":
            case "StringColumn":
                column.Type = {
                    kind: "string",
                    suffix : child.props.suffix
                };
                break;
            case "Toggle":
            case "ToggleColumn":
                column.Type = {
                    kind: "toggle",
                    options: {
                        on: child.props.on,
                        off: child.props.off
                    }
                };
                break;
            case "Button":
            case "ButtonColumn":
                column.Type = {
                    kind: "button",
                    label: child.props.label
                };
                break;
            case "Enum":
            case "EnumColumn":
                column.Type = {
                    kind: "enum",
                    options: getOptionsFromChildren(child.props.children)
                };
                break;
            case "Color":
            case "ColorColumn":
                column.Type = {
                    kind: "color"
                };
                break;
            default:
                console.warn("Unknown column type: " + childType.name + " or " + childType.displayName);
                return;
        }

        columns.push(column);
    });

    return columns;
}

export function getOptionsFromProps(options : Option[]) {
    const len = options.length;
    const opts = new Array(len);

    for (let i = 0; i < len; i++) {
        if (typeof options[i] === "string") {
            opts[i] = {
                value: options[i],
                description: options[i]
            };
        }
        else {
            opts[i] = options[i];
        }
    }

    return opts;
}

export function getOptionsFromChildren(children : any[]) : SelectOption[] {
    let options : SelectOption[] = [];

    React.Children.forEach(children, (child : React.ReactChild, index : number) => {
        if (typeof child === "object") {
            if (child && child.type) {
                const childType : any = child.type;

                const typeName = childType.name || childType.displayName;
                if (typeName === "Option") {
                    options.push({
                                     value: child.props.value,
                                     description: child.props.description || child.props.value
                                 })
                }
            }
        }
    });

    return options;
}

export function getOptions(cell) : any[] {
    if (!cell.type || !cell.type.options) {
        return [];
    }

    return getOptionsFromProps(cell.type.options);
}
