#! /usr/bin/python -O
# -*- coding: utf-8 -*-
#
#    CANTA Theme Editor - An editor for themes used in CANTA
#    Copyright (C) 2008  A. Kattner
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


import sys
import os
import soya
import soya.pudding.ext.fpslabel
import soya.cube
import soya.sphere
import getopt
from canta.theme.theme_manager import ThemeManager
from canta.theme.editor import Editor
from canta.display.core_init import CoreInit

def usage():
    print "\n| CANTA Theme Viewer"
    print "| USAGE: python <theme name> <res x> <res y> <fs>"
    print "|   - theme name: the name of the directory where the theme files are stored"
    print "|   - res x: resolution, e.g. 1024"
    print "|   - res y: resolution, e.g. 768"
    print "|   - fs: fullscreen 1 or 0\n|"
    print "|   Example: python run_view_theme.py mozart 1024 768 0"

def main():
    if len(sys.argv) != 5:
        usage()
        sys.exit(2)

    # app dir:
    app_dir = os.path.dirname(sys.argv[0])

    # theme name:
    theme_name = sys.argv[1]

    # app name:
    APP_NAME = 'Canta Theme Viewer'

    # version:
    VERSION = '0.1'

    RESIZEABLE = True

    # DEBUG flag:
    DEBUG = False

    # get platform info:
    PLATFORM = sys.platform

    # on Windows systems:
    if 'win32' in PLATFORM:
        import PIL.PngImagePlugin
        import PIL.JpegImagePlugin

    window_title = APP_NAME + ' ' + VERSION

    res_x =  int(sys.argv[2])
    res_y =  int(sys.argv[3])
    fs = int(sys.argv[4])

    soya.init(title=window_title, width=res_x, height=res_y, \
            fullscreen=fs, resizeable=RESIZEABLE)

    # Enable/disable soya's auto (blender model) importer:
    soya.AUTO_EXPORTERS_ENABLED = True

    theme_path = os.path.join(app_dir, 'media', 'themes', \
            theme_name, 'media')
    soya.path.append(theme_path)

    scene = soya.World()

    light = soya.Light(scene)
    light.set_xyz(0.0, 7.7, 17.0)
    light.cast_shadow = True

    camera = soya.Camera(scene)

    moveable = True
    rotating = False
    if moveable:
        from canta.cameras.movable_camera import MovableCamera
        camera = MovableCamera(app_dir, scene, DEBUG)
    if rotating:
        from canta.cameras.spinning_camera import SpinningCamera
        cube = soya.Body(scene, soya.cube.Cube().to_model())
        cube.visible = True
        camera = SpinningCamera(scene, cube)

    #camera = soya.TravelingCamera(scene)

    camera.set_xyz(0.0, 0.0, 15.0)
#	camera.fov = 90.0
#	camera.ortho = True
#	camera.set_xyz(-0.5, -0.5, 4.0)
#	camera.rotate_y(-90.0)
#	camera.set_xyz(0.0, -0.5, 7.0)

    # Create the editor world.
    world = Editor(scene, camera)

    # load the theme config settings:
    theme_mgr = ThemeManager(world)
    theme_dir = os.path.join(app_dir, 'media', 'themes', theme_name)
    theme_mgr.get_theme(theme_name, theme_dir)
    theme_mgr.show_theme(theme_name)

# TEST OBJECTS
#	wall_model = soya.World()
#	wall_face = soya.Face(wall_model, [
#		soya.Vertex(wall_model, -5.0, 0.0, -5.0),
#		soya.Vertex(wall_model, -5.0, 0.0,  5.0),
#		soya.Vertex(wall_model,  5.0, 0.0,  5.0),
#		soya.Vertex(wall_model,  5.0, 0.0, -5.0),
#		])
#	wall_face.double_sided = 1
#	wall = soya.Body(world, wall_model.to_model())
#	wall.set_xyz(0.0, 0.0, 0.0)

#	cube = soya.Body(scene, soya.cube.Cube().to_model())
#	sphere = soya.Body(scene, soya.sphere.Sphere().to_model())

#	world.filename = "theme"
#	world.save()

    soya.set_root_widget(camera)
    soya.MainLoop(scene).main_loop()



if __name__ == '__main__': main()
