# Copyright (c) 2016-2020 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from datetime import timedelta
from dateutil.relativedelta import *
from constants import COLORS
import pytz

utc = pytz.UTC


def get_months_in_range(r):
    months = []
    start_of_month = r[0].replace(day=1)
    while start_of_month <= r[1]:
        months.append(start_of_month.month)
        start_of_month = start_of_month + relativedelta(months=1)
    return months


def make_row(month, userinfo, group):
    row = "| "
    active_this_month = True if "active" not in userinfo else False
    right_removed = False
    right_added = False
    if "active" in userinfo:
        for r in userinfo["active"]:
            if month.month in get_months_in_range(r):
                # User was active at some point this month
                active_this_month = True
            if month.month == r[0].month:
                # Right was added this month
                right_added = True
            if month.month == r[1].month:
                # Right was removed this month
                right_removed = True

    if userinfo["actions"][month.month] == 0 and not active_this_month:
        row += "\n"
    else:
        # User was active this month
        row += "{:,}".format(userinfo["actions"][month.month])
        if right_added:
            row += '<ref name="+{}" />\n'.format(group)
        if right_removed:
            row += '<ref name="-{}" />\n'.format(group)
        if not right_added and not right_removed:
            row += "\n"
    return row


def make_table(users, groups, months, group):
    rows = []
    for user in sorted(users.keys(), key=str.lower):
        userinfo = users[user]
        color = None
        if user in groups["arbs"]:
            color = COLORS["arb"]
        elif user in groups["ombuds"]:
            color = COLORS["ombuds"]
        row = '|- style="background: {}"\n'.format(color) if color else "|-\n"
        row += "| {}\n".format(user)
        for month in months:
            row += make_row(month, userinfo, group)
        rows.append(row)
    rows.append(gather_stats(users, months))
    return "".join(rows)


def gather_stats(users, months):
    totals = {month.month: 0 for month in months}
    for user in users:
        for month, actions in users[user]["actions"].items():
            totals[month] += actions
    row = "|-\n!Total\n! {:,}\n".format(totals[months[0].month])
    for i in range(1, 6):
        this_month = totals[months[i].month]
        last_month = totals[months[i - 1].month]
        percent_change = (this_month - last_month) / last_month * 100
        color = "red" if percent_change < 0 else "green"
        prefix = "" if percent_change < 0 else "+"
        row += "! {count:,} {{{{fontcolor|{color}|({prefix}{percent_change}%)}}}}\n".format(
            count=totals[months[i].month],
            color=color,
            prefix=prefix,
            percent_change=round(percent_change, 1),
        )
    return row


def write_table(*tables):
    file_str = "\n\n\n".join(tables)
    with open("stats.txt", "w+") as f:
        f.write(file_str)

# Copyright (c) 2016-2020 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from datetime import timedelta
from dateutil.relativedelta import *
from constants import COLORS
import pytz

utc = pytz.UTC


def get_months_in_range(r):
    months = []
    start_of_month = r[0].replace(day=1)
    while start_of_month <= r[1]:
        months.append(start_of_month.month)
        start_of_month = start_of_month + relativedelta(months=1)
    return months


def make_row(month, userinfo, group):
    row = "| "
    active_this_month = True if "active" not in userinfo else False
    right_removed = False
    right_added = False
    if "active" in userinfo:
        for r in userinfo["active"]:
            if month.month in get_months_in_range(r):
                # User was active at some point this month
                active_this_month = True
            if month.month == r[0].month:
                # Right was added this month
                right_added = True
            if month.month == r[1].month:
                # Right was removed this month
                right_removed = True

    if userinfo["actions"][month.month] == 0 and not active_this_month:
        row += "\n"
    else:
        # User was active this month
        row += "{:,}".format(userinfo["actions"][month.month])
        if right_added:
            row += '<ref name="+{}" />\n'.format(group)
        if right_removed:
            row += '<ref name="-{}" />\n'.format(group)
        if not right_added and not right_removed:
            row += "\n"
    return row


def make_table(users, groups, months, group):
    rows = []
    for user in sorted(users.keys(), key=str.lower):
        userinfo = users[user]
        color = None
        if user in groups["arbs"]:
            color = COLORS["arb"]
        elif user in groups["ombuds"]:
            color = COLORS["ombuds"]
        row = '|- style="background: {}"\n'.format(color) if color else "|-\n"
        row += "| {}\n".format(user)
        for month in months:
            row += make_row(month, userinfo, group)
        rows.append(row)
    rows.append(gather_stats(users, months))
    return "".join(rows)


def gather_stats(users, months):
    totals = {month.month: 0 for month in months}
    for user in users:
        for month, actions in users[user]["actions"].items():
            totals[month] += actions
    row = "|-\n!Total\n! {:,}\n".format(totals[months[0].month])
    for i in range(1, 6):
        this_month = totals[months[i].month]
        last_month = totals[months[i - 1].month]
        percent_change = (this_month - last_month) / last_month * 100
        color = "red" if percent_change < 0 else "green"
        prefix = "" if percent_change < 0 else "+"
        row += "! {count:,} {{{{fontcolor|{color}|({prefix}{percent_change}%)}}}}\n".format(
            count=totals[months[i].month],
            color=color,
            prefix=prefix,
            percent_change=round(percent_change, 1),
        )
    return row


def write_table(*tables):
    file_str = "\n\n\n".join(tables)
    with open("stats.txt", "w+") as f:
        f.write(file_str)

# Copyright (c) 2016-2020 <NAME> # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # User was active at some point this month # Right was added this month # Right was removed this month # User was active this month