from util_test import *
from PILB import Image, ImageDraw

def reset():
    #dragged out of config file
    const.minimum_contest_height_inches = 1.
    const.vote_target_horiz_offset_inches = .07
    const.allowed_corner_black_inches = .185
    const.ballot_dpi = 151
    const.dpi = 151
    const.oval_width_inches = .34
    const.oval_height_inches = .2
    const.allowed_tangent = .05 
    const.candidate_text_horiz_offset_inches = .43
    const.vote_intensity_threshold = 135
    const.dark_pixel_threshold = 1000

reset()
import Ballot
import hart_ballot

new = hart_ballot.HartBallot

def flip_test():
    reset()
    testrec = (200, 200, 400, 400)
    im = Image.open("testdata/hart1.jpg").convert("RGB")
    im = im.crop(testrec)
    hb = new("testdata/hart1_flipped.jpg", None)
    hbim = hb.pages[0].image.crop(testrec)
    #XXX we flip now but the jpg artifacting disallows this comparison
    for a, b in zip(im.tostring(), hbim.tostring()):
        assert a == b

def find_landmarks_test():
    reset()#WHERE are we setting page xoff, yoff . . .
    b = new("testdata/hart1.jpg", NilXtnz())
    r, x, y = b.find_landmarks(b.pages[0])
    assert r == 0.0
    assert x == 88
    assert y == 116

def get_layout_code_test():
    reset()
    b = new("testdata/hart1.jpg", NilXtnz())
    b.FindLandmarks(b.pages[0]) #need page landmarks
    lc = b.get_layout_code(b.pages[0])
    assert lc == "10000900200047"

def extract_VOP_test():#XXX move if/when pushed to superclass
    block = Image.new("RGB", (100, 100), "#fff")
    ImageDraw.Draw(block).rectangle((20, 20, 60, 60), fill="#000")
    concho, all = CONCHO(
        (0,0,100,100, "prop", "description", 
            (20, 20, "vop", True, False, False
            )
        )
    )
    hb = hart_ballot.HartBallot
    ballot = hb.__new__(hb)
    ballot.extensions = NilXtnz()
    ballot.dpi = 100
    ballot.oval_size = (40, 40)
    ballot.vote_target_horiz_offset = 20 #???
    const.vote_intensity_threshold = 200
    const.dark_pixel_threshold = 741
    page = Ballot.Page(dpi=ballot.dpi, image=block)
    page.as_template("prec", concho)

    x, y, ss, im, v, w, a = ballot.extract_VOP(
        page,
        lambda *a: a,
        1.0,
        all[0],
    )
    assert x == 20
    assert y == 26 #includes crop offset
    assert v
    assert not w
    assert not a

def get_layout_test():
    reset()
    rets = new("testdata/hart1.jpg", NilXtnz()).ProcessPages()
    assert concho_vs_vd(mg, rets) #mg is defined below



#mostly machine generated by testdata/hart1.py
_, mg = CONCHO(
    (95, 119, 454, 216, "None", "OFFICIAL BALLOT//HUMBOLDT COUNTYcomma CALIFC//November O2 2010//",
    ),
    (95, 219, 454, 617, "None", "BOARD OF EQUALIZATION MEMBER//DISTRICT 1//Vote for Ons//",
        (102, 299, "BETTY T YEE DEM//Equalization Board Member//", True, False, False),
        (102, 360, "KENNITA WATSON LIB//Retired Qualiiy Engineer//", False, False, False),
        (102, 420, "SHERILL BORG PF//Community Development Ofhcer//", False, False, False),
        (102, 481, "KEVIN R SCOTT REP//Small Business Owner//", False, False, False),
        (102, 541, "Writei n//", False, False, False),
    ),
    (95, 620, 454, 1115, "None", "UNITED STATES SENATOR//Vols for One//",
        (102, 676, "DUANE ROBERTS GRN//Ccmmunilv Volunteer//", False, False, False),
        (102, 736, "BARBARA BOXER DEM//United States Senator//", True, False, False),
        (102, 796, "GAIL K LIGHTFOOT LIB//Retired Nurse//", False, False, False),
        (102, 856, "MARSHA FEINLAND PF//Retxred Teacher//", False, False, False),
        (102, 917, "CARLY FIORINA REP//Business Executove//", False, False, False),
        (102, 977, "EDWARD C NOONAN AIP//Computer Store Owner//", False, False, False),
        (102, 1038, "Writein//", False, False, False),
    ),
    (95, 1116, 454, 1516, "None", "ummzn sTATEs REPRESENKQE 7//DISTRICT1//Vcle fur One//",
        (102, 1197, "MIKE RODRIGUES LIB//Entrepreneur/Inventor//", False, False, False),
        (102, 1258, "MIKE THOMPSON DEM//Member of Ccnnress//", True, False, False),
        (102, 1318, "CAROL WOLMAN GRN//PsvchiatristIM D/Writer//", False, False, False),
        (102, 1379, "LOREN HANKS REP//Reserve Militarv Oificer//", False, False, False),
        (102, 1440, "Writei n//", False, False, False),
    ),
    (95, 1518, 454, 1797, "None", "STATE SENATOR DISTRICT 2//Vote fur Ons//",
        (102, 1573, "LAWRENCE R REP//WIESNER//Accuuntant/Educator/Parent//", False, False, False),
        (102, 1660, "NOREEN EVANS DEM//Assemblvmember//", True, False, False),
        (102, 1721, "Writei n//", False, False, False),
    ),
    (95, 1800, 454, 1999, "None", "/",
    ),
    (460, 116, 821, 215, "None", "wr CONSOLIDATE//RNIA//Precin//",
    ),
    (460, 218, 821, 495, "None", "STATE ASSEMBLY MEMBER//DISTRICT 1//Vnls fur One//",
        (467, 298, "KAREN BROOKS REP//Smalr Busnness Owner//", False, False, False),
        (467, 359, "WESLEY CHESBRO DEM//Assemblvmember First District//", True, False, False),
        (467, 420, "Writein//", False, False, False),
    ),
    (460, 498, 821, 775, "None", "SUPERINTENDENT OF PUBLIC//INSTRUCTION//Vols Iur One//",
        (467, 578, "TOM TORLAKSON//Teacher/Cahfomia Leuislator//", True, False, False),
        (467, 639, "LARRY ACEVES//Retired School Superintendent//", False, False, False),
        (467, 700, "Writein//", False, False, False),
    ),
    (460, 778, 821, 835, "None", "Judicial//",
        (467, 815, " commacomma__r_w//", False, False, False),
    ),
    (460, 841, 821, 873, "None", "VOTE YES OR NO FOR EACH OFFICE//",
    ),
    (460, 876, 821, 935, "None", "JUDICIALcomma Chiswustico oltha Suprama//Court//",
    ),
    (460, 936, 821, 1116, "None", "Shall Assocwate Justicecomma Coun of Appealcomma//Third Annellate Disric TANI G//CANTILSAKAUYE be elected to the//cmce for the term nrovided bv Iavf//",
        (467, 1044, "YES//", False, False, False),
        (467, 1080, "NO//", True, False, False),
    ),
    (460, 1119, 821, 1177, "None", "JUDICIALcomma Associate Justicecomma Supreme//Court//",
    ),
    (460, 1180, 821, 1333, "None", "Shall Associate Justlce MING W CHIN//be elected 0 the omce for the term//urcvidec bv lawsquot//",
        (467, 1262, "YES//", False, False, False),
        (467, 1297, "NO//", True, False, False),
    ),
    (460, 1336, 821, 1395, "None", "JUDICIALcomma Associate Justice Supreme//Court//",
    ),
    (460, 1398, 821, 1551, "None", "Shall Associate Juslice CARLOS R//MORENO be elected to me cfhce for the//term umvided bv law//",
        (467, 1479, "YES//", True, False, False),
        (467, 1515, "NO//", False, False, False),
    ),
    (460, 1554, 821, 1613, "None", "JUDICIALcomma Associate Justice Court of//Appeal District 1 Division 1//",
    ),
    (460, 1614, 821, 1770, "None", "Shall Associate Jusiice KATHLEEN M//BANKE be elected to the oftice furthe//term provided by law//",
        (467, 1697, "YES//", False, False, False),
        (467, 1732, "NO//", True, False, False),
    ),
    (460, 1771, 821, 1829, "None", "JUDICIAL Associate Justicecomma Cuurt f//Appaalcomma District 1comma Division 1//",
    ),
    (460, 1832, 821, 1986, "None", "Shall Associate Justice ROBERT Lcomma//DONDERO be elected to the office for the//term provided by law//",
        (467, 1914, "YES//", False, False, False),
        (467, 1950, "NO//", True, False, False),
    ),
    (460, 1989, 821, 2000, "None", "",
    ),
    (827, 117, 1185, 215, "None", "D GENERAL ELECTION//t 4E31//",
    ),
    (827, 217, 1185, 432, "None", "JUDICIAL Associate Justice Court of//Appealcomma District 1 Division 2//Shall Associate Justice JAMES R//LAMEDEN be elected to the office for the//term provided by law//",
        (834, 360, "YES//", False, False, False),
        (834, 396, "NO//", True, False, False),
    ),
    (827, 435, 1185, 494, "None", "JUDICIAL Associate Justice Court of//Appealcomma Districi 1comma Division 3//",
    ),
    (827, 495, 1185, 650, "None", "Shall Asscciale Justice MARTIN J//JENKINS be elected to the office for the//term provided by law//",
        (834, 578, "YES//", False, False, False),
        (834, 614, "NO//", True, False, False),
    ),
    (827, 653, 1185, 711, "None", "JUDICIAL Associate Justice Court cl//Appeal Districi 1 Division 3//",
    ),
    (827, 714, 1185, 867, "None", "Shall Assuciate Justice PETER J//SIGGINS be elected tothe omce for the//term nrovided bv lava///",
        (834, 795, "YES//", False, False, False),
        (834, 831, "NO//", True, False, False),
    ),
    (827, 870, 1185, 929, "None", "JUDICIAL Associate Justice Court of//Appealcomma District 1 Division 4//",
    ),
    (827, 930, 1185, 1085, "None", "Shall Associate Justice TIMOTHY A//REARDON he elected 0 the oftice for the//term nrcvided bv law//",
        (834, 1012, "YES//", False, False, False),
        (834, 1048, "NO//", True, False, False),
    ),
    (827, 1088, 1185, 1146, "None", "JUDICIAL Assuciaie Justice Court of//Appeal Distrid 1comma Division 5//",
    ),
    (827, 1149, 1185, 1304, "None", "Shall Associate Justice TERENCE L//BRUINIERS be elected tothe oftice for//the term provided by Iavf//",
        (834, 1230, "YES//", False, False, False),
        (834, 1266, "NO//", True, False, False),
    ),
    (827, 1305, 1185, 1363, "None", "JUDICIAL Associate Justicecomma Coun of//Appealcomma District 1comma Division 5//",
    ),
    (827, 1366, 1185, 1520, "None", "Shall Associate Justice HENRY E//NEEDHAM JR be elected to the uftice//for the term urcvxded bv law//",
        (834, 1449, "YES//", False, False, False),
        (834, 1484, "NO//", True, False, False),
    ),
    (827, 1523, 1185, 1528, "None", "",
    ),
    (827, 1529, 1185, 1580, "None", "IC0untv//",
        (834, 1560, "/", False, False, False),
    ),
    (827, 1586, 1185, 1838, "None", "SUPERVISOR DISTRICT 4//Vote for One//",
        (834, 1641, "VIRGINIA BASS//Eureka Mavor//", False, False, False),
        (834, 1702, "BONNIE NEELY//Humboldt Counlv Suuerviscr//", True, False, False),
        (834, 1762, "Writein//", False, False, False),
    ),
    (827, 1841, 1185, 2001, "None", "dquot/H  /v MM /MJ //",
    ),
)
