import amanith
import unittest
import os
from os.path import join
import pygame
import sys
import math
from OpenGL.GL import *
import time

class AmanithTest(unittest.TestCase):
  def testKernel(self):
    k = amanith.GKernel()
    assert k.ClassID().IDName() == "GKernel"

  def testVectors(self):
    k = amanith.GVector4(1, 2, 3, 4)
    assert k[0] == 1.0
    assert k[1] == 2.0
    assert k[2] == 3.0
    assert k[3] == 4.0
    k[3] = 5
    assert k[3] == 5.0

  def testMatrices(self):
    m = amanith.GMatrix22()
    m.Set(1, 2, 3, 4)
    m[1, 1] = 5
    assert m[0, 1] == 2
    assert m[1, 1] == 5

  def testQuaternions(self):
    m = amanith.GQuaternion(1, 2, 3, 4)
    m[0] = 5
    assert m[1] == 2
    assert m[0] == 5

  def testOutputParameters(self):
    board = amanith.GOpenGLBoard(0, 0, 640, 480)
    p = board.Projection()
    assert p[1] == 640.0 and p[3] == 480.0

  def setUp(self):
    self.amanithPath = "AMANITHDIR" in os.environ and os.environ["AMANITHDIR"] or "amanith"

  def testPixelMap(self):
    k = amanith.GKernel()
    k.LoadPlugins(join(self.amanithPath, "plugins"), True)
    i = amanith.GPixelMap(k)

    try:
      i.Load("no-such-file")
      raise "Exception not thrown"
    except RuntimeError:
      pass

    assert i.Load(join(self.amanithPath, "data", "amanith32.png")) is None

  def testDrawBoard(self):
    pygame.display.init()
    surface = pygame.display.set_mode((640, 480), pygame.OPENGL | pygame.DOUBLEBUF)
    board = amanith.GOpenGLBoard(0, 0, surface.get_size()[0], surface.get_size()[1])
    
    k = amanith.GKernel()
    k.LoadPlugins(join(self.amanithPath, "plugins"), True)
    img = amanith.GPixelMap(k)
    img.Load(join(self.amanithPath, "data", "amanith32.png"))
    pat = board.CreatePattern(img, amanith.G_HIGH_IMAGE_QUALITY, amanith.G_PAD_TILE)
    pat.SetLogicalWindow((128, 128), (256 + 128, 256 + 128))
    
    keys = [
      amanith.GKeyValue(0.0, (1.0, 0.0, 0.0, .5)),
      amanith.GKeyValue(0.5, (0.0, 1.0, 0.0, .5)),
      amanith.GKeyValue(1.0, (0.0, 0.0, 1.0, .5)),
    ]
    grad = board.CreateLinearGradient((255 + 128, 255 + 128), (128, 128), keys)
    board.SetRenderingQuality(amanith.G_HIGH_RENDERING_QUALITY)
  
    board.SetFillEnabled(True)
    board.SetStrokeEnabled(False)
    board.SetStrokeWidth(8)
    board.Clear(1.0, 1.0, 1.0, True)
    board.SetFillPaintType(amanith.G_PATTERN_PAINT_TYPE)
    board.SetFillPattern(pat)
    board.DrawRoundRectangle(128, 128, 255 + 128, 255 + 128, 30, 30)
    board.SetFillPaintType(amanith.G_GRADIENT_PAINT_TYPE)
    board.SetFillGradient(grad)
    board.SetStrokeEnabled(True)
    board.SetFillCompOp(amanith.G_LIGHTEN_OP)
    board.DrawRoundRectangle(128, 128, 255 + 128, 255 + 128, 30, 30)
    pygame.display.flip()

if __name__ == "__main__":
  unittest.main()
