#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re

WIDTH = 9.777

def generate_from_num(num):
    sn_path = f"icons/sn-{num}.svg"
    s_path = f"icons/s-{num}.svg"
    generate(sn_path, s_path)

    sn_path = f"icons/sn-{num}-negative.svg"
    s_path = f"icons/s-{num}-negative.svg"
    generate(sn_path, s_path)

def generate(sn_path, s_path):
    with open(sn_path, "r") as sn_f:
        sn_svg = sn_f.read()

    s_svg = convert_svg(sn_svg)

    with open(s_path, "w") as s_f:
        s_f.write(s_svg)

def convert_svg(sn_svg):
    # Remove N
    path_match = list(re.finditer("path d=\"(.*?)\"", sn_svg))[1]
    path_pos = path_match.span()
    path_pos = (path_pos[0] + 8, path_pos[1] - 1)
    path = path_match.group(1)

    N_pos = list(re.finditer("M.*?Z", path))[1].span()
    path = path[:N_pos[0]] + path[N_pos[1]:]

    # Move digits left
    S, path = path.split("Z", 1)
    digits = list(re.finditer("[MHL](\d+(\.\d+)?)", path))[::-1]

    for digit in digits:
        pos = digit.span()
        x = float(digit.group(1))
        x -= WIDTH
        path = path[:pos[0]+1] + str(x) + path[pos[0] + len(digit.group(1)):]

    path = S + "Z" + path
    sn_svg = sn_svg[:path_pos[0]] + path + sn_svg[path_pos[1]:]

    # Change color
    sn_svg = sn_svg.replace("#FFDE15", "#fff")

    return sn_svg

if __name__ == "__main__":
    for i in range(1, 100):
        generate_from_num(i)
    
    generate("icons/sn.svg", "icons/s.svg")
    generate("icons/sn-negative.svg", "icons/s-negative.svg")