import { tpl } from './widgetPlugTpl';
import { Messager } from '@core/component/msgDistribute';
import {fetch} from '@core/component/httpClient';
import Message from '@core/component/message';
import LinkTool from '../../linkTool';
import SLD from '@core/util/domain';
const WidgetPlug = (($) => {
    
    const DefaultConfig = {
        tpl,
    };
    
    
    class WidgetPlug {
        
        constructor(config) {
            
            this._config = this._getConfig(config);
            this.el = this._config.node;
            this.$el = $(this.el);
            this._renderDom();
            this._config.onDomUpdate();
            this._addEvent();
        }
        
        
        _addEvent() {
            
            let _this = this;
            
            
            this.linkChangeKey = Messager.on('linkChange', (req, res) => {
                this.$el.find('.J_widgetLink').val(res.link)
            })
            
            
            
            this.$el.on('input.widgetLink change.widgetLink', '.J_widgetLink', function() {
                let input = $(this);
                let link = input.val();
                let linkType = LinkTool.Type.WidgetPlug;
                let originalData = link;
                Messager.trigger('linkChange', { link, linkType, originalData });
            })
            
            
            this.$el.on('click.widgetLink', '.J_ok', async () => {
                let url = $('.J_linkInput').val();
                if (url === '') {
                    errorHint('链接不能为空');
                    return false;
                }
                url = url.trim();
                let reg = /^plugin(-private)?:\/\//;
                if(!reg.test(url)){
                    errorHint('请输入合法的链接');
                }else{
                    this.$el.find('.J_widgetLink').val(url).trigger('keyup');
                    Messager.trigger('confirm');
                }
                
                function errorHint(msg) {
                    $('.J_widgetLink').addClass('cd-error');
                    $('.J_errorMsg').text(msg);
                }
            })
            
            this.$el.on('focus', '.J_widgetLink', function() {
                $(this).removeClass('cd-error');
            })
            
            
            this.$el.on('click.widgetLink', '.J_cancel', function() {
                Messager.trigger('cancel');
            })
            
        }
        
        
        _getConfig(config) {
            return $.extend(true, {}, DefaultConfig, config);
        }
        
        
        _renderDom() {
            let config = this._config;
            let html = template.render(config.tpl, config);
            this.$el.html(html);
            
        }
        
        
        dispose() {
            Messager.off(this.linkChangeKey);
            this.$el.off('click.widgetLink').off('linkChange.widgetLink').off('change.widgetLink')
        }
    }
    
    return WidgetPlug;
    
})($);
export default WidgetPlug;