(function() {
    'use strict';

    tinymce.PluginManager.add('pinnerpress_follow', function(editor, url) {
        
        // Add button to toolbar
        editor.addButton('pinnerpress_follow', {
            title: 'Insert Pinterest Follow Button',
            icon: 'icon dashicons-pinterest',
            onclick: function() {
                // Open dialog
                editor.windowManager.open({
                    title: 'Pinterest Follow Button',
                    width: 400,
                    height: 500,
                    body: [
                        {
                            type: 'textbox',
                            name: 'url',
                            label: 'Pinterest URL',
                            placeholder: 'https://www.pinterest.com/yourusername',
                            value: ''
                        },
                        {
                            type: 'textbox',
                            name: 'text',
                            label: 'Button Text',
                            value: 'Follow on Pinterest'
                        },
                        {
                            type: 'listbox',
                            name: 'size',
                            label: 'Button Size',
                            values: [
                                { text: 'Small', value: 'small' },
                                { text: 'Medium', value: 'medium' },
                                { text: 'Large', value: 'large' }
                            ],
                            value: 'medium'
                        },
                        {
                            type: 'listbox',
                            name: 'align',
                            label: 'Alignment',
                            values: [
                                { text: 'Left', value: 'left' },
                                { text: 'Center', value: 'center' },
                                { text: 'Right', value: 'right' }
                            ],
                            value: 'left'
                        },
                        {
                            type: 'textbox',
                            name: 'color',
                            label: 'Button Color',
                            value: '#E60023',
                            placeholder: '#E60023'
                        },
                        {
                            type: 'textbox',
                            name: 'text_color',
                            label: 'Text Color',
                            value: '#ffffff',
                            placeholder: '#ffffff'
                        },
                        {
                            type: 'checkbox',
                            name: 'show_icon',
                            label: 'Show Pinterest Icon',
                            checked: true
                        },
                        {
                            type: 'checkbox',
                            name: 'new_tab',
                            label: 'Open in New Tab',
                            checked: true
                        }
                    ],
                    onsubmit: function(e) {
                        var data = e.data;
                        
                        // Validate URL
                        if (!data.url || data.url.trim() === '') {
                            alert('Please enter a Pinterest URL.');
                            return false;
                        }
                        
                        // Build shortcode
                        var shortcode = '[pinnerpress_follow';
                        shortcode += ' url="' + data.url.trim() + '"';
                        
                        if (data.text && data.text !== 'Follow on Pinterest') {
                            shortcode += ' text="' + data.text + '"';
                        }
                        
                        if (data.size && data.size !== 'medium') {
                            shortcode += ' size="' + data.size + '"';
                        }
                        
                        if (data.align && data.align !== 'left') {
                            shortcode += ' align="' + data.align + '"';
                        }
                        
                        if (data.color && data.color !== '#E60023') {
                            shortcode += ' color="' + data.color + '"';
                        }
                        
                        if (data.text_color && data.text_color !== '#ffffff') {
                            shortcode += ' text_color="' + data.text_color + '"';
                        }
                        
                        if (!data.show_icon) {
                            shortcode += ' show_icon="false"';
                        }
                        
                        if (!data.new_tab) {
                            shortcode += ' new_tab="false"';
                        }
                        
                        shortcode += ']';
                        
                        // Insert shortcode
                        editor.insertContent(shortcode);
                    }
                });
            }
        });
        
        // Add menu item
        editor.addMenuItem('pinnerpress_follow', {
            text: 'Pinterest Follow Button',
            context: 'insert',
            icon: 'icon dashicons-pinterest',
            onclick: function() {
                editor.execCommand('mceButton_pinnerpress_follow');
            }
        });
    });
})(); 