'use strict'

const client   = require('cheerio-httpcli');
const request  = require('request');
const settings = require('./settings');
const post     = require('./post');
const itunes   = require('./itunes');

const Usen = function() {
  this.param = {
    npband: 'A',
    npch  : '44',
    nppage: 'yes',
    _     : ''
  };
  this.nowPlaying  = '';
  this.botName     = settings.BOT_NAME;
  this.webhookUrl  = settings.WEB_HOOK_URL;
  this.channelName = '';
};

Usen.prototype = {

  postNowPlaying: function() {

    this.param._ = new Date().getTime();

    client.fetch('http://music.usen.com/usencms/search_nowplay1.php', this.param)
    .then((result) => {
      return result.$('.np-now li').text().replace(/[ａ-ｚＡ-Ｚ０-９＝！？＄＋＊％＆，．（）]/g, (s) => {
        return String.fromCharCode(s.charCodeAt(0) - 0xFEE0);
      }).replace(/　/g, ' ').replace(/’/g, "'").replace(/－/g, '-');
    })
    .then((np) => {
       if (np !== this.nowPlaying) {
        itunes.getArtworkUrl(np.split('／')[0].trim())
        .then((url) => {
          if (url !== '') {
            post.messegeWithAttachment(this.webhookUrl, url, np, this.channelName);
          } else {
            post.message(this.webhookUrl, np, this.channelName);
          }
        })
        .catch(() => {
          post.message(this.webhookUrl, np, this.channelName);
        })
        this.nowPlaying = np;
      }
    });
  },

  setBand: function(band) {
    this.param.npband = band;
  },

  getBand: function() {
    return this.param.npband;
  },

  setChannel: function(channel) {
    this.param.npch = channel;
  },

  getChannel: function() {
    return this.param.npch;
  },

  getNowPlaying: function() {
    return this.nowPlaying;
  },

  getChannelName: function() {
    return this.channelName;
  },

  getChannelTitle: function() {
    return new Promise((resolve, reject) => {
      client.fetch('http://music.usen.com/channel/' + this.param.npband + this.param.npch + '/')
      .then((result) => {
        return result.$('.detail-title > h2').text();
      })
      .then((title) => {
        this.channelName = title;
        post.message(this.webhookUrl, title, this.botName)
        .then((result) => {
          resolve();
        })
        .catch((err) => {
          reject();
        });
      });
    });
  }
}

module.exports = Usen;

@@ -986,16 +986,50 @@
 '%EF%BC%8F')%5B0%5D.
+replace(/%5E%5C((%5B0-9%5D+%E4%BD%8D%7C%E6%B3%A8%E7%9B%AE%E6%9B%B2)%5C)/, '').
 trim())%0A

add music name regex