#! /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_COOH
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()

  fs = build_formula_sixth(svg)
  k0 = fm.add_subformula(fs, 0,   0, [0,0])
  k1 = fm.add_subformula(fs, 0,  60, [0,0])
  k2 = fm.add_subformula(fs, 0, 120, [0,0])
  k3 = fm.add_subformula(fs, 0, 180, [0,0])
  k4 = fm.add_subformula(fs, 0, 240, [0,0])
  k5 = fm.add_subformula(fs, 0, 300, [0,0])
  fm.add_bond(k0+0,k1+0,1.5)
  fm.add_bond(k1+0,k2+0,1.5)
  fm.add_bond(k2+0,k3+0,1.5)
  fm.add_bond(k3+0,k4+0,1.5)
  fm.add_bond(k4+0,k5+0,1.5)
  fm.add_bond(k5+0,k0+0,1.5)
  return fm

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_sixth(svg) :
  "Builds 1/6 of the molecule (one carbon and one carboxyl) pointing horizontally."

  fs = mformula.obj()

  Ac = 0           # Angle of carboxyl with horizontal (degrees).
  Ao = 90          # Opening angle of oxygens in carboxyl (degrees).
  Ah = Ao/2.0 - Ac # Bend angle of C-O-H to keep the OH horizontal (degrees).

  cb = svg.rel_bond_length(1.5) # Length of aromatic bonds.
  ub = svg.rel_bond_length(2.0) # Relative length of single bonds.

  # Assume that the benzene ring stays hexagonal:
  R = cb                    # Radius of benzene ring.
  c30 = cos(30*pi/180)
  s30 = sin(30*pi/180)

  p0 = [+1*R, 00*R]                        # A carbon of benzene.
  p1 = rn.add(p0, rn.scale(ub, [+1, 00]))  # Carbon of carboxyl

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

  fg = build_formula_COOH(svg, Ao,Ah, 0)
  k1 = fs.add_subformula(fg, 0, Ac, p1)

  fs.add_bond(k0,k1,1.0)
  return fs


