(function(wp) {
    var registerBlockType = wp.blocks.registerBlockType;

    var TextControl   = wp.components.TextControl;
    var SelectControl = wp.components.SelectControl;
    var ColorPalette  = wp.components.ColorPalette;

    var el = wp.element.createElement;

    var withState = wp.compose.withState;

    var __ = wp.i18n.__;

    var types = [{
        'label' : __('All', 'resideo-plugin'),
        'value' : '0'
    }];
    var statuses = [{
        'label' : __('All', 'resideo-plugin'),
        'value' : '0'
    }];

    function getObjectProperty(obj, prop) {
        var prop = typeof prop !== 'undefined' ? prop : '';
        var obj = typeof obj !== 'undefined' ? obj : '';

        if(!prop || !obj) {
            return '';
        }

        var ret = obj.hasOwnProperty(prop) ? ( String(obj[prop]) !== ''? obj[prop] : '') : '';

        return ret;
    }

    function getAttr(s, n) {
        n = new RegExp(n + '=\"([^\"]+)\"', 'g').exec(s);
        return n ? window.decodeURIComponent(n[1]) : '';
    };

    jQuery.ajax({
        type: 'POST',
        dataType: 'json',
        url: ajaxurl,
        data: {
            'action': 'resideo_get_types_statuses'
        },
        success: function(data) {
            if (data.getts === true) {
                for (var i = 0; i < data.types.length; i++) {
                    types.push({ 
                        'label' : data.types[i].name,
                        'value' : data.types[i].term_id
                    });
                }
                for (var i = 0; i < data.statuses.length; i++) {
                    statuses.push({
                        'label' : data.statuses[i].name,
                        'value' : data.statuses[i].term_id
                    });
                }
            }
        },
        error: function(errorThrown) {}
    });

    function FeaturedPropertiesControl(props) {
        var attributes    = props.attributes;
        var setAttributes = props.setAttributes;
        var setState      = props.setState;
        var className     = props.className;
        var isSelected    = props.isSelected;

        var data_content = attributes.data_content;
        var data         = window.decodeURIComponent(data_content);
        var data_json    = jQuery.parseJSON(data);

        var title     = getObjectProperty(data_json, 'title');
        var subtitle  = getObjectProperty(data_json, 'subtitle');
        var cta_label = getObjectProperty(data_json, 'cta_label');
        var cta_link  = getObjectProperty(data_json, 'cta_link');
        var cta_color = getObjectProperty(data_json, 'cta_color');
        var type      = getObjectProperty(data_json, 'type');
        var status    = getObjectProperty(data_json, 'status');
        var number    = getObjectProperty(data_json, 'number');
        var margin    = getObjectProperty(data_json, 'margin');
        var layout    = getObjectProperty(data_json, 'layout');

        var renderCTAColorSelector = el('div',
            {
                className: 'components-base-control'
            },
            el('div',
                {
                    className: 'components-base-control__field'
                },
                el('fieldset',
                    {},
                    el('legend',
                        {},
                        el('div',
                            {},
                            el('span',
                                {
                                    className: 'components-base-control__label'
                                },
                                __('CTA Button Color', 'resideo-plugin'),
                            )
                        )
                    ),
                    el(ColorPalette,
                        {
                            value: cta_color,
                            colors: [
                                { name: 'Pale pink', color: '#f58fa8' },
                                { name: 'Vivid red', color: '#cd3235' },
                                { name: 'Luminous vivid orange', color: '#fd6a29' },
                                { name: 'Luminous vivid amber', color: '#fcb738' },
                                { name: 'Light green cyan', color: '#80dab7' },
                                { name: 'Vivid green cyan', color: '#2bcd89' },
                                { name: 'Pale cyan blue', color: '#8fd2f9' },
                                { name: 'Vivid cyan blue', color: '#0896df' },
                                { name: 'Vivid purple', color: '#975cdb' },
                                { name: 'Very light gray', color: '#eeeeee' },
                                { name: 'Cyan bluish gray', color: '#abb9c2' },
                                { name: 'Very dark gray', color: '#333333' }
                            ],
                            onChange: function(value) {
                                data_json.cta_color = value;
                                setAttributes({ data_content: encodeURIComponent(JSON.stringify(data_json)) });
                            }
                        }
                    )
                )
            )
        );

        var featuredPropertiesOptions = [
            el('div', 
                {
                    className: 'row'
                },
                el('div', 
                    {
                        className: 'col-xs-12 col-sm-6'
                    },
                    el(TextControl, 
                        {
                            label: __('Title', 'resideo-plugin'),
                            value: title,
                            placeholder: __('Enter title', 'resideo-plugin'),
                            onChange: function(value) {
                                data_json.title = value;
                                setAttributes({ data_content: encodeURIComponent(JSON.stringify(data_json)) });
                            }
                        }
                    )
                ),
                el('div', 
                    {
                        className: 'col-xs-12 col-sm-6'
                    },
                    el(TextControl, 
                        {
                            label: __('Subtitle', 'resideo-plugin'),
                            value: subtitle,
                            placeholder: __('Enter subtitle', 'resideo-plugin'),
                            onChange: function(value) {
                                data_json.subtitle = value;
                                setAttributes({ data_content: encodeURIComponent(JSON.stringify(data_json)) });
                            }
                        }
                    )
                )
            ),
            el('div', 
                {
                    className: 'row'
                },
                el('div', 
                    {
                        className: 'col-xs-12 col-sm-6'
                    },
                    el(TextControl, 
                        {
                            label: __('CTA Button Text', 'resideo-plugin'),
                            value: cta_label,
                            placeholder: __('Enter CTA button text', 'resideo-plugin'),
                            onChange: function(value) {
                                data_json.cta_label = value;
                                setAttributes({ data_content: encodeURIComponent(JSON.stringify(data_json)) });
                            }
                        }
                    )
                ),
                el('div', 
                    {
                        className: 'col-xs-12 col-sm-6'
                    },
                    el(TextControl, 
                        {
                            label: __('CTA Button Link', 'resideo-plugin'),
                            value: cta_link,
                            placeholder: __('Enter CTA button link', 'resideo-plugin'),
                            onChange: function(value) {
                                data_json.cta_link = value;
                                setAttributes({ data_content: encodeURIComponent(JSON.stringify(data_json)) });
                            }
                        }
                    )
                )
            ),
            renderCTAColorSelector,
            el('div', 
                {
                    className: 'row'
                },
                el('div', 
                    {
                        className: 'col-xs-12 col-sm-6'
                    },
                    el(SelectControl, 
                        {
                            label: __('Type', 'resideo-plugin'),
                            value: type,
                            options: types,
                            onChange: function(value) {
                                data_json.type = value;
                                setAttributes({ data_content: encodeURIComponent(JSON.stringify(data_json)) });
                            }
                        }
                    )
                ),
                el('div', 
                    {
                        className: 'col-xs-12 col-sm-6'
                    },
                    el(SelectControl, 
                        {
                            label: __('Status', 'resideo-plugin'),
                            value: status,
                            options: statuses,
                            onChange: function(value) {
                                data_json.status = value;
                                setAttributes({ data_content: encodeURIComponent(JSON.stringify(data_json)) });
                            }
                        }
                    )
                )
            ),
            el('div', 
                {
                    className: 'row'
                },
                el('div', 
                    {
                        className: 'col-xs-12 col-sm-6'
                    },
                    el(TextControl, 
                        {
                            label: __('Number of Properties', 'resideo-plugin'),
                            value: number,
                            placeholder: __('Enter number of properties', 'resideo-plugin'),
                            onChange: function(value) {
                                data_json.number = value;
                                setAttributes({ data_content: encodeURIComponent(JSON.stringify(data_json)) });
                            }
                        }
                    )
                ),
                el('div',
                    {
                        className: 'col-xs-12 col-sm-6'
                    },
                    el(SelectControl, 
                        {
                            label: __('Margin', 'resideo-plugin'),
                            value: margin,
                            options: [
                                { label: __('No', 'resideo-plugin'), value: 'no' },
                                { label: __('Yes', 'resideo-plugin'), value: 'yes' }
                            ],
                            onChange: function(value) {
                                data_json.margin = value;
                                setAttributes({ data_content: encodeURIComponent(JSON.stringify(data_json)) });
                            }
                        }
                    )
                )
            ),
            el('div', 
                {
                    className: 'row'
                },
                el('div', 
                    {
                        className: 'col-xs-12 col-sm-6'
                    },
                    el(SelectControl, 
                        {
                            label: __('Layout', 'resideo-plugin'),
                            value: layout,
                            options: [
                                { label: __('Layout 1', 'resideo-plugin'), value: '1' },
                                { label: __('Layout 2', 'resideo-plugin'), value: '2' },
                                { label: __('Layout 3', 'resideo-plugin'), value: '3' }
                            ],
                            onChange: function(value) {
                                data_json.layout = value;
                                setAttributes({ data_content: encodeURIComponent(JSON.stringify(data_json)) });
                            }
                        }
                    )
                )
            )
        ];

        if (isSelected) {
            return el('div', 
                {
                    className: className
                },
                el('h3', 
                    {
                        className: 'featured-properties-placeholder-header'
                    },
                    title
                ),
                el('h4', 
                    {
                        className: 'featured-properties-placeholder-subheader'
                    },
                    subtitle
                ),
                featuredPropertiesOptions
            );
        } else {
            return el('div', 
                {
                    className: className
                },
                el('h3', 
                    {
                        className: 'featured-properties-placeholder-header'
                    },
                    title
                ),
                el('h4', 
                    {
                        className: 'featured-properties-placeholder-subheader'
                    },
                    subtitle
                ),
                el('div', 
                    {
                        className: 'featured-properties-placeholder-img'
                    }
                )
            );
        }
    }

    registerBlockType('resideo-plugin/featured-properties', {
        title: __('Featured Properties', 'resideo-plugin'),
        description: __('Resideo featured properties block.', 'resideo-plugin'),
        icon: {
            src: 'star-filled',
            foreground: '#007cba',
        },
        category: 'widgets',
        keywords: [
            __('featured', 'resideo-plugin'),
            __('properties', 'resideo-plugin'),
            __('listings', 'resideo-plugin')
        ],
        attributes: {
            data_content: {
                type: 'string',
                default: '%7B%22title%22%3A%22%22%2C%22subtitle%22%3A%22%22%2C%22cta_label%22%3A%22%22%2C%22cta_link%22%3A%22%22%2C%22cta_color%22%3A%22%23333333%22%2C%22type%22%3A%220%22%2C%22status%22%3A%220%22%2C%22number%22%3A%223%22%2C%22margin%22%3A%22no%22%2C%22layout%22%3A%221%22%7D'
            }
        },
        edit: withState({})(FeaturedPropertiesControl),
        save: function(props) {
            return null;
        },
    });
})(window.wp);