War Times
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jdehorty
// @version=5

indicator('War Times', overlay=true, scale=scale.none, max_lines_count=500)

import trevor1617/WarCalendar/1 as calendar

// ==================
// ==== Settings ====
// ==================

show_war_start = input.bool(defval = true, title = "📅 Start", inline = "Start", group="⚙️ Settings", tooltip="War start date")
c_warstart = input.color(color.new(color.red, 50), title = "Color", group="⚙️ Settings", inline = "Start")

show_legend = input.bool(true, "Show Legend", group="⚙️ Settings", inline = "Legend", tooltip="Show the color legend for the economic calendar events.")
use_alt_date_resolution = input.bool(false, "Use Alternative Date Resolution", group="⚙️ Settings", inline = "AlternativeDateResolution", tooltip="Use alternative date resolution for the economic calendar events. May help in scenarios where the daily timeframe is off by a day.")

// =======================
// ==== Dates & Times ====
// =======================

getUnixTime(eventArr, i) => 
    int t = array.get(eventArr, i)
    switch 
        timeframe.isdaily => timestamp(year(t), month(t), dayofmonth(t)-timeframe.multiplier+1, 00, 00, 00)
        timeframe.isweekly => timestamp(year(t), month(t), dayofmonth(t)-7*timeframe.multiplier+1, 00, 00, 00)
        timeframe.ismonthly => timestamp(year(t), month(t)-timeframe.multiplier+1, 00, 00, 00, 00)
        timeframe.isminutes and timeframe.multiplier > 60  => timestamp(year(t), month(t), dayofmonth(t), hour(t)-timeframe.multiplier/60+1, minute(t), second(t))
        => timestamp(year(t), month(t), dayofmonth(t), hour(t), minute(t), second(t))

// Note: The following is an alternative implementation of getUnixTime. It is useful for independently double-checking the resulting vertical lines of the above function.

getUnixTimeAlt(eventArr, i) => 
    int t = array.get(eventArr, i)
    switch 
        timeframe.isdaily and timeframe.multiplier >= 1 => t - timeframe.multiplier*86400000 // -n days
        timeframe.isweekly => t - timeframe.multiplier*604800000 // -n week(s)
        timeframe.ismonthly => t - timeframe.multiplier*2592000000 // -n month(s)
        timeframe.isminutes and timeframe.multiplier > 60 => t - timeframe.multiplier*60000 // -n minutes
        => t
    
// Note: An offset of -n units is needed to realign events with the timeframe in which they occurred
if show_war_start
    warstartArr = calendar.warstart()
    for i = 0 to array.size(warstartArr) - 1
        unixTime = use_alt_date_resolution ? getUnixTimeAlt(warstartArr, i) : getUnixTime(warstartArr, i)
        line.new(x1=unixTime, y1=high, x2=unixTime, y2=low, extend=extend.both,color=c_warstart, width=2, xloc=xloc.bar_time)

// ================
// ==== Legend ====
// ================
if show_legend
    var tbl = table.new(position.top_right, columns=1, rows=8, frame_color=#151715, frame_width=1, border_width=2, border_color=color.new(color.black, 100))
    units = timeframe.isminutes ? "m" : ""
    if barstate.islast
        table.cell(tbl, 0, 0, syminfo.ticker + ' | ' + str.tostring(timeframe.period) + units, text_halign=text.align_center, text_color=color.gray, text_size=size.normal)
        table.cell(tbl, 0, 1, 'War Start', text_halign=text.align_center, bgcolor=color.black, text_color=color.new(c_warstart, 10), text_size=size.small)
