'use strict';
const r = require('../services/reddit');
if (!r) {
  return;
}
const usernoteHelper = require('../services/usernote-helper');
const SUBREDDIT = 'pokemontrades';

const SPAM_COOLDOWN = 60 * 60 * 6; // The number of seconds allowed between threads
const TRADE_TYPE_CSS_CLASSES = new Set(['tradeback', 'dexevo', 'redeem', 'swsh', 'smusum', 'xyoras', 'letsgo']);
const GIVEAWAY_TYPE_CSS_CLASSES = new Set(['contest', 'giveaway']);

const threadsByUser = new Map();
const handledThreadIds = new Set();

const getSpamReply = (newThread, oldThread) => `Hello /u/${newThread.author.name},

Sorry, but this thread has been removed because it violates [rule 7](/r/pokemontrades/wiki/rules). In the future, please do not post trade threads more often than once per 6 hours. (Your [other thread](${oldThread.url}) was posted more recently than 6 hours ago.)

*This action was performed by a bot. If you think there was some mistake, please click the "report" button next to this comment, and a human will take a look at your post shortly.*`;

module.exports = {
  period: 60,
  onStart: false,
  task ({bot: {nick}}) {
    return r.getSubreddit(SUBREDDIT).getNew()
      .filter(thread => !handledThreadIds.has(thread.id))
      .then(threads => threads.reverse())
      .each(thread => handledThreadIds.add(thread.id))
      .each(thread => {
        if (!threadsByUser.has(thread.author.name)) {
          threadsByUser.set(thread.author.name, []);
        }
      }).mapSeries(thread => {
        const oldThread = threadsByUser.get(thread.author.name).find(oldThread => threadBreaksRules(thread, oldThread));

        if (oldThread && thread.approved_by === null) {
          return thread.remove().reply(getSpamReply(thread, oldThread)).distinguish().return(
            `[Flood notification]: The thread <${thread.url}> by /u/${thread.author.name} breaks the flood rule, and has been removed. (Old thread: <${oldThread.url}> )`
          ).tap(() => usernoteHelper.addNote({mod: nick, user: thread.author.name, subreddit: thread.subreddit.display_name, note: '6 hour rule', link: `l,${thread.id}`, index: 0, fromChannel: null}));
        } else {
          threadsByUser.set(
            thread.author.name,
            threadsByUser.get(thread.author.name)
              .filter(oldThread => oldThread.created_utc + SPAM_COOLDOWN > Date.now() / 1000)
              .concat({url: thread.url, created_utc: thread.created_utc, link_flair_css_class: thread.link_flair_css_class})
          );
        }
      }).filter(response => response);
  }
};

function threadBreaksRules (thread, oldThread) {
  return oldThread.created_utc + SPAM_COOLDOWN > thread.created_utc && (
    TRADE_TYPE_CSS_CLASSES.has(thread.link_flair_css_class) && TRADE_TYPE_CSS_CLASSES.has(oldThread.link_flair_css_class) ||
    GIVEAWAY_TYPE_CSS_CLASSES.has(thread.link_flair_css_class) && GIVEAWAY_TYPE_CSS_CLASSES.has(oldThread.link_flair_css_class)
  );
}

@@ -266,34 +266,29 @@
  TRADE_TYPE_
-CSS_CLASSE
+FLAIR
 S = new Set(
@@ -293,75 +293,71 @@
 t(%5B'
-tradeback', 'dexevo', 'redeem', 'swsh', 'smusum', 'xyoras', 'letsgo
+SWSH', 'LGPE', 'SMUSUM', 'XYORAS', 'Home', 'Tradeback', 'Redeem
 '%5D);
@@ -377,26 +377,21 @@
 AY_TYPE_
-CSS_CLASSE
+FLAIR
 S = new 
@@ -400,20 +400,9 @@
 t(%5B'
-contest', 'g
+G
 ivea
@@ -2412,25 +2412,20 @@
 k_flair_
-css_class
+text
 : thread
@@ -2436,25 +2436,20 @@
 k_flair_
-css_class
+text
 %7D)%0A     
@@ -2646,34 +2646,29 @@
  TRADE_TYPE_
-CSS_CLASSE
+FLAIR
 S.has(thread
@@ -2675,33 +2675,28 @@
 .link_flair_
-css_class
+text
 ) && TRADE_T
@@ -2695,34 +2695,29 @@
  TRADE_TYPE_
-CSS_CLASSE
+FLAIR
 S.has(oldThr
@@ -2731,25 +2731,20 @@
 k_flair_
-css_class
+text
 ) %7C%7C%0A   
@@ -2754,34 +2754,29 @@
 VEAWAY_TYPE_
-CSS_CLASSE
+FLAIR
 S.has(thread
@@ -2787,25 +2787,20 @@
 k_flair_
-css_class
+text
 ) && GIV
@@ -2814,18 +2814,13 @@
 YPE_
-CSS_CLASSE
+FLAIR
 S.ha
@@ -2846,17 +2846,12 @@
 air_
-css_class
+text
 )%0A  

Switch flairs to use flair text instead