#! /usr/bin/python3 -t


PROG_DESC = "Outputs an SVG 'roadkill' diagram for said molecule"

import math; from math import sqrt,sin,cos,pi
import rn
import sys, os, re
import mformula
import mformula_oxocarbon; from mformula_oxocarbon import build_formula_COO_1neg
import mformula_svg



def image_name(style):
  fname = __file__
  imgname = re.sub(r'.*[/]', "", fname[0:-3]) + \
    "_style" + style
  return imgname

def image_descr(style):
  fname = __file__
  imgname = re.sub(r'.*[/]', "", fname[0:-3])
  descr = "Structural formula of " + re.sub(r'[_]', " ", imgname)
  descr += "\n" + \
    "\n" + \
    mformula_svg.style_descr(style)
  return descr

def build_formula(svg):

  fm = mformula.obj()
  c30 = cos(30*pi/180)
  s30 = sin(30*pi/180)

  av = 1.0                 # Valence of bond from C to OO.
  ab = svg.rel_bond_length(av)  # Relative length of bond from C to OO.

  eq = -1.0                # Mean charge of terminal peroxo O.
  ev = 1.0                 # Valence of O-O bond.
  eb = svg.rel_bond_length(ev)  # Relative length of O-O bond.

  # Atom centers:
  p0 = [0,0]                            # Carbon.

  p1 = rn.add(p0, rn.scale(ab, [-1, 00]))

  p2 = rn.add(p1, rn.scale(eb,  [-s30, -c30]))

  fg = build_formula_COO_1neg(svg,120)
  k0 = fm.add_subformula(fg, 0, 0, p0)

  k1 = fm.add_atom("O", p1,  0,1)
  k2 = fm.add_atom("O", p2, -1,1)
  if (svg.style[0] == 'S') :
    # Add the "-" symbol next to the {k2} oxygen:
    p2q = rn.add(p2, rn.scale(0.65*eb,  [-s30, -c30]))
    k2q = fm.add_atom("+", p2q, -1,1)

  fm.add_bond(k0,k1,av)
  fm.add_bond(k1,k2,ev)

  return fm


