#! /bin/usr/python3
# Test program for module {seam}
# Last edited on 2021-05-31 16:07:16 by jstolfi

import seam
import seam_example
import move
import move_parms
import path
import block
import block_example
import contact
import contact_example
import palette
import hacks
import job_parms
import rn
import pyx
import sys
from math import sqrt, sin, cos, floor, ceil, pi, nan, inf

parms = job_parms.typical_js()
parms['solid_raster_width'] = 1.00
parms['contour_trace_width'] = 0.50

mp_jump = move_parms.make_for_jumps(parms)
mp_cont = move_parms.make_for_contours(parms)
mp_fill = move_parms.make_for_fillings(parms)
wd_fill = move_parms.width(mp_fill)

def test_basics():
  sys.stderr.write("--- testing {make,get_contacts,side,which_side,is_directed,set_name,get_name,tag_names} ---\n")
  BCS, OPHS, TRS, JMS = block_example.misc_A(mp_fill, mp_jump)
  bc0 = BCS[0]; MVS0 = block.moves(bc0)
  bc1 = BCS[1]; MVS1 = block.moves(bc1)
  bc2 = BCS[2]; MVS2 = block.moves(bc2)
  
  CTS01 = contact.from_move_lists(MVS0, MVS1, 0, 0)
  CTS12 = contact.from_move_lists(MVS1, MVS2, 0, 0)
  
  for drc in False, True:
    sys.stderr.write("  ... drc = %d ...\n" % drc)
    sm = seam.make(bc0, bc1, CTS01, drc)
    sys.stderr.write("\n")
    contacts = True
    seam.show(sys.stderr, sm, contacts, 0, 0, 0)
    sys.stderr.write("\n")
    assert seam.is_directed(sm) == drc
    assert tuple(CTS01) == seam.get_contacts(sm)
    assert seam.side(sm, 0) == bc0
    assert seam.side(sm, 1) == bc1
    assert seam.which_side(bc0, sm) == 0
    assert seam.which_side(bc1, sm) == 1
    
  smA = seam.make(bc0, bc1, CTS01, False)
  smB = seam.make(bc1, bc2, CTS12, True)

  assert seam.get_name(smA) == "S?"
  seam.set_name(smA, "Zipper")
  assert seam.get_name(smA) == "Zipper"
  
  sys.stderr.write("applying {seam.tag_names}:\n")
  seam.tag_names([smA, smB], "Tag.")
  assert seam.get_name(smA) == "Tag.Zipper"
  assert seam.get_name(smB) == "Tag.S?"

  return
  # ----------------------------------------------------------------------
  
def test_show():

  sys.stderr.write("--- testing {show,show_list} ---\n")

  SMS, BCS = seam_example.misc_A(mp_fill)
  
  sys.stderr.write("  ... {show} ...\n")
  wna = 5
  wnc = 3
  for k in range(4):
    sm = SMS[k]
    contacts = (k % 2 != 0)
    seam.show(sys.stderr, sm, contacts, 4, wna, wnc)
    wna = wna + 2
    wnc = max(0, wnc - 1)
    sys.stderr.write("\n")
  
  sys.stderr.write("  ... {show_list} ...\n")
  seam.show_list(sys.stderr, SMS, True, 2)
  return 
  # ----------------------------------------------------------------------

def test_plot_to_files():
  sys.stderr.write("--- testing {plot_to_files} ---\n")

  tag = "A"
  SMS, BCS = seam_example.misc_A(mp_fill)
  nsm = len(SMS)
  nbc = len(BCS)
  
  rwd = 0.80
  wd_axes = 0.05*wd_fill
  CLRS = hacks.trace_colors(nbc)
  matter = True

  clr_ct = pyx.color.rgb(1.000, 0.200, 0.000)
  wd_ct = 2*wd_axes

  fname = ("tests/out/seam_TST_plot_to_files_%s" % tag)
  seam.plot_to_files(fname, SMS, clr_ct, wd_ct, BCS, CLRS, rwd, wd_axes, matter)
  return
  # ----------------------------------------------------------------------
  
def test_plot_single():
  sys.stderr.write("--- testing {plot_single} ---\n")

  tag = "A"
  
  SMS, BCS = seam_example.misc_A(mp_fill)
  sys.stderr.write("  ... all seams ...\n")
  seam.show_list(sys.stderr, SMS, True, 2)
  nsm = len(SMS)
  nbc = len(BCS)
  
  sm = SMS[2]
  sys.stderr.write("  ... chosen seam ...\n")
  seam.show(sys.stderr, sm, True, 2, 0, 0)
  sys.stderr.write("\n")
  contact.show_list(sys.stderr, seam.get_contacts(sm), 4)
 
  B = block.bbox(BCS)
  B = rn.box_join(B, seam.bbox(SMS))
  
  dp = (0, 0)
  c, szx, szy = hacks.make_canvas(B, dp, True, True, 1, 2)
  
  rwd = 0.80
  wd_axes = 0.05*wd_fill

  dpk = dp
  for restrict in False, True:
    subtag = ("rs%s" % "FT"[restrict])
    if restrict:
      oph0 = block.choice(seam.side(sm,0),0)
      oph1 = block.choice(seam.side(sm,1),0)
      axes_ph = False
      dots_ph = False
      arrows_ph = True
      matter_ph = True
      path.plot_standard(c, [oph0, oph1], dpk, None, None, rwd, wd_axes, axes_ph, dots_ph, arrows_ph, matter_ph)
    else:
      oph0 = None; oph1 = None
    
    clr_ct = pyx.color.rgb(1.000, 0.000, 1.000)
    wd_ct = 2*wd_axes
    tics = True
    seam.plot_single(c, sm, dpk, clr_ct, wd_ct, oph0, oph1, tics)
    dpk = rn.add(dpk, (0,szy))
  
  fname = ("tests/out/seam_TST_plot_single_%s" % tag)
  hacks.write_plot(c, fname)
  return
  # ----------------------------------------------------------------------
 
def test_bbox():
  sys.stderr.write("--- testing {bbox} ---\n")

  SMS, BCS = seam_example.misc_A(mp_fill)
  nsm = len(SMS)
  nbc = len(BCS)
  
  for sm in SMS:
    B0 = seam.bbox(sm)
    B1 = None
    for ct in seam.get_contacts(sm):
      Bk = contact.bbox(ct)
      B1 = rn.box_join(B1, Bk)
    assert B0 == B1
  return
  # ----------------------------------------------------------------------

def test_print_contact_table():
  sys.stderr.write("--- testing {print_contact_table} ---\n")

  SMS, BCS = seam_example.misc_A(mp_fill)
  nsm = len(SMS)
  nbc = len(BCS)
  
  MVS = [] # List of all moves in all blocks.
  for bc in BCS:
    MVS.append(block.moves(bc))
  
  wr = sys.stderr
  seam.print_contact_table(wr, BCS, SMS, MVS)
  return
  # ----------------------------------------------------------------------

test_plot_single()
test_plot_to_files()
test_basics()
test_show()

test_print_contact_table()
# test_min_max_tcool()
