/*
A widget that displays a posting streak, powered by your ilo.so dashboard.

You will need to put in your counter ID in the widget's Parameter field.
You can find your counter ID by clicking the ↗ Share icon in your ilo account.

Sign up to ilo at https://ilo.so

Version 1.1.0 (November 11, 2024)
by Dan Rowden (@dr)
*/


// Some colours you can change

const chartColor = "#c88e69"
const chartOpacity = 0.3
const widgetBgColor = "#fff7f2"
const secondaryColor = '#6d6d6d'
const primaryColor = '#1e272e'
const highlightColor = '#b77a54'
const warningColor = '#C51D17'


// Fonts

const smallText = new Font("HelveticaNeue", 14);
const largeText = new Font("HelveticaNeue-Medium", 16);



// =====================================================================
// Here starts all the widget code. Feel free to edit below if you want!
// Share your widget on Twitter! I'm @dr :)
// =====================================================================

let twitterParameter = args.widgetParameter.split(':')
let twitterUsername = twitterParameter[0]
let apiKey = twitterParameter[1]

let url = "https://api.ilo.so/v1/streak/"+twitterUsername+"/?api_key="+apiKey
let req = new Request(url)
const json = await req.loadJSON()
const yesterday_streak = parseInt(json['yesterday_streak'])
const tweets_today = parseInt(json['tweets_today'])
const current_streak = parseInt(json['current_streak'])
const longest_streak = parseInt(json['longest_streak'])
const streak = tweets_today == 0 ? yesterday_streak : current_streak


// Create the widget
let w = new ListWidget()
w.backgroundColor = new Color(widgetBgColor);
w.url = 'https://ilo.so/'+twitterUsername+'/'

var refreshDate = Date.now() + 1000*60*5 // Refresh after at least 5 minutes
w.refreshAfterDate = new Date(refreshDate)


// ilo icon
let icon = await loadImage('https://ilo.so/static/favicon.png')
let iconImg = w.addImage(icon)
iconImg.imageSize = new Size(20,20)

w.addSpacer(16)


// Show username
const usernameStr = '@' + twitterUsername
textUsername = w.addText(usernameStr)
textUsername.textColor = new Color(secondaryColor);
textUsername.font = smallText;

w.addSpacer(4)


// Show current follower count
const currentStr = streak + " day streak"
textStreak = w.addText(currentStr)
textStreak.textColor = new Color(primaryColor);
textStreak.font = largeText

w.addSpacer(4)


// Show today's tweet count
let tweetsTodayStr = (tweets_today > 0 ? '+' : '') + tweets_today + " tweet" + ((tweets_today == 1) ? "" : "s") + " today"
let tweetColor = highlightColor
if (tweets_today == 0) {
  tweetColor = warningColor
}
textTweets = w.addText(tweetsTodayStr)
textTweets.textColor = new Color(tweetColor);
textTweets.font = smallText;

w.addSpacer(4)


// Show today's tweet count
const longestStreakStr = "Longest: " + longest_streak + " days"
textLongest = w.addText(longestStreakStr)
textLongest.textColor = new Color(secondaryColor);
textLongest.font = smallText;


// Download an image from a given url
async function loadImage(imgUrl) {
  const req = new Request(imgUrl)
  return await req.loadImage()
}


if (config.runsInWidget) {
  Script.setWidget(w)
} else {
  await w.presentSmall()
}

if (config.runsWithSiri) {
  Speech.speak('Your current streak is '+streak+' days.')
}


Script.complete()