#! /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
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)
  a90 = 90*pi/180

  oq = -1.0 # Mean charge of O atom.
  ov =  1.0 # Mean valence of C-O bond.
  ob = svg.rel_bond_length(ov) # Relative length of C-O bond.

  # Atom centers:
  p0 = [0,0]
  p1 = rn.add(p0, rn.scale(ob, [-c30, -s30]))
  p2 = rn.add(p0, rn.scale(ob, [+c30, -s30]))

  k0 = fm.add_atom("C", p0, 0,0)

  k1 = fm.add_atom("O", p1, -1,1)
  k2 = fm.add_atom("O", p2, -1,1)

  if (svg.style[0] == 'S') :
    # Add the "-1/2" symbols next to the oxygens:
    p1q = rn.add(p1, rn.scale(0.65*ob, [00, -1]))
    p2q = rn.add(p2, rn.scale(0.65*ob, [00, -1]))

    k1q = fm.add_atom("+", p1q, -1,1)
    k2q = fm.add_atom("+", p2q, -1,1)

  fm.add_bond(k0,k1,ov)
  fm.add_bond(k0,k2,ov)
  fm.add_free_bond(k0,a90,2)

  return fm

