var $ = window.jQuery;
(function() {
  "use strict";

  this.SwitchWitch = function(breakpoint) {
    this.breakpoint = breakpoint || Infinity;
  };

  /**
   * Function: isMobile
   */
  this.SwitchWitch.prototype.isMobile = function(option) {
    return window.innerWidth < this.breakpoint;
  };

  /**
   * Function: switchImage
   */
  this.SwitchWitch.prototype.switchImages = function(option) {
    var option = option || {},
        selector = 'img[src$="_pc.png"], img[src$="_pc.jpg"], img[src$="pc.jpeg"], img[src$="pc.gif"], img[src$="pc.tiff"], img[src$="_sp.png"], img[src$="_sp.jpg"], img[src$="sp.jpeg"], img[src$="sp.gif"], img[src$="sp.tiff"]',
        bgSelector = '[style^="background-image:"]',
        flag = '',
        breakpoint = this.breakpoint,
        customEvent = option.customEvent || '',
        $target = option.target ? $(option.target) : $('body');

    var onResize = function() {
      if (window.innerWidth < breakpoint && (flag === 'pc' || flag === '')) {
        flag = 'sp';
        $target.find(selector).each(function() {
          var imgPath = $(this).attr('src').replace(/_pc\.(.*)$/, '_sp.$1');
          $(this).attr('src', imgPath);
        });
        $target.find(bgSelector).each(function() {
          var imgPath = $(this).css('background-image').replace(/_pc\.(.*)$/, '_sp.$1');
          $(this).css('background-image', imgPath);
        });
      }
      else if (window.innerWidth >= breakpoint && flag === 'sp' || flag === '') {
        flag = 'pc';
        $target.find(selector).each(function() {
          var imgPath = $(this).attr('src').replace(/_sp\.(.*)$/, '_pc.$1');
          $(this).attr('src', imgPath);
        });
        $target.find(bgSelector).each(function() {
          var imgPath = $(this).css('background-image').replace(/_sp\.(.*)$/, '_pc.$1');
          $(this).css('background-image', imgPath);
        });
      }
    };
    onResize();
    $(window).on('resize', function() {
      onResize();
    });
    $(window).on(customEvent, function() {
      onResize();
    })
  };

  /**
   * Function: autoLink
   */
  this.SwitchWitch.prototype.autoLink = function(option) {
    var option = option === void 0 ? {} : option
    var target = option.target || '.sww-auto-link'
    var $facebookButton = $(target + '.facebook-button')
    var $tweetButton = $(target + '.tweet-button')
    var $lineButton = $(target + '.line-button')

    // Facebook
    $facebookButton.each(function() {
      var api = 'https://www.facebook.com/sharer/sharer.php'
      var url = encodeURIComponent($(this).data('url') || '')
      
      url = url ? ('?u=' + url) : url
      
      $(this).attr({
        href: api + url
      })
    })

    // Twitter
    $tweetButton.each(function() {
      var api = 'https://twitter.com/intent/tweet'
      var url = encodeURIComponent($(this).data('url') || '')
      var text = encodeURIComponent($(this).data('text') || '')
      var hash = encodeURIComponent($(this).data('hashtags') || '')

      url = url ? ('url=' + url) : url
      text = text ? ('text=' + text) : text
      hash = hash ? ('hashtags=' + hash) : hash

      if (url) {
        url = '?' + url
        text = text ? ('&' + text) : text
        hash = hash ? ('&' + hash) : hash
      } else if (text) {
        text = '?' + text
        hash = hash ? ('&' + hash) : hash
      } else if (hash) {
        hash = '?' + hash
      }

      $(this).attr({
        href: api + url + text + hash
      })
    })

    // Line
    $lineButton.each(function() {
      var api = 'http://line.me/R/msg/text/'
      var text = encodeURIComponent($(this).data('text') || '')
      
      text = text ? ('?' + text) : text
      
      $(this).attr({
        href: api + text
      })
    })
  }

  /**
   * Function: selectize
   */
  this.SwitchWitch.prototype.selectize = function(option) {
    var option = option === void 0 ? {} : option
    var target = option.target || '.sww-selectable'
    var label = option.label || 'selected'

    $(document).on('click', target, function() {
      var groupId = $(this).data('group-id')
      var isRadio = $(this).data('radio') || false

      console.log(groupId)

      if (groupId) {
        var targetGroup = target + '[data-group-id="' + groupId + '"]'

        if (isRadio) {
          $(targetGroup).removeClass(label)
          $(this).addClass(label)
        } else {
          $(this).toggleClass(label)
        }
      }
    })
  }
}).call(this);