#!/usr/bin/env node
const axios = require('axios');
const config = require('./config');
// const sendmail = require('sendmail')({silent: true});
const EmailService = require('./email');
const emailService1 = new EmailService('sohu');
const emailService2 = new EmailService('163');
const play = require('./sound');
const Utils = require('./utils');
const ItemService = require('./item');
const itemService = new ItemService();

let lastList = {};
const apiUrl = `http://steamcommunity.com/market/listings/730/${config.itemHash}/render?start=0&count=100&currency=23&language=english`;
const targetUrl = `https://steamcommunity.com/market/listings/730/${config.itemHash}`;
(async () => {
    try {
        lastList = await getItemList();
        run();
    } catch (e) {
        console.log(e);
    }
})();

async function run() {
    try {
        await extractPage();
    } catch (e) {
        console.log(e);
    }
    // schedule下一个扫描
    setTimeout(() => run(),
        Utils.randomIntFromInterval(config.interval.min, config.interval.max) * 1000
    );
}

async function extractPage() {
    const newList = await getItemList();
    const newItems = calcNewItems(newList, lastList);
    lastList = newList;
    let msg = '';
    for (let item of newItems) {
        const floatInfo = await itemService.getFloat(item);
        msg += `${Utils.getLocaleDateTime()} -- 磨损值： ${floatInfo}, and 价格 : ${itemService.getPrice(item)} \n<br/>`;
    }
    if (newItems.length) {
        play(config.soundFilePath);
        msg += `...点击<a href="${targetUrl}" target="_blank">这里前往</a><br/>`
        // let's notify user
        emailService1.sendEmail(config.emailSubject, msg);
        emailService2.sendEmail(config.emailSubject, msg);
    }
}

async function getItemList() {
    let res = await axios.get(apiUrl);
    console.log(`${Utils.getLocaleDateTime()},拿到新的列表，共有${res.data.total_count}个物品。`);
    return res.data.listinginfo;
}


function calcNewItems(newList, oldList) {
    const result = [];
    const newListIds = Object.keys(newList);
    if (oldList) {
        const newListIds = Object.keys(newList);
        newListIds.forEach((oldId) => {
            if (!oldList[oldId]) {
                result.push(newList[oldId]);
            }
        });
    }
    return result;
}
@@ -1329,16 +1329,19 @@
 msg += %60
+%3Cp%3E
 $%7BUtils.
@@ -1421,16 +1421,20 @@
 e(item)%7D
+%3C/p%3E
  %5Cn%3Cbr/%3E
@@ -1521,16 +1521,19 @@
 msg += %60
+%3Cp%3E
 ...%E7%82%B9%E5%87%BB%3Ca 
@@ -1577,16 +1577,20 @@
 %E9%87%8C%E5%89%8D%E5%BE%80%3C/a%3E%3C
+/p%3E%3C
 br/%3E%60%0A  

add p tag to email content