from __future__ import division
import math
import operator
import os

import numpy
import pyglet
from pyglet.gl import *
import yaml

from edge import *
from const import *
from model import *
from state import *

class Util(object):
    '''Utility class with various helpful functions.'''

    @staticmethod
    def showHelp():
        '''Prints command line usage help.'''
        helpLines = [
            ("-h, --help", "Shows this help."),
            ("-d", "Displays debug information in-game. Use multiple times for more info."),
            ("-p", "Performance mode. Removes features, adds FPS. Use multiple times for more FPS."),
            ("--worldtype TYPE", "Choose a world type to generate. Options: {}".format(Const.WORLD_TYPES)),
            ("--seed STRING", "Specify a world seed to use. Uses system time by default.")
        ]
        for line in helpLines:
            print "{:<16}\t{}".format(*line)

    @staticmethod
    def loadModels(modeltype):
        '''Loads flyweight models from yaml files given the model type'''
        models = {}
        defaultmodel = None
        counter = -1
        for dict in yaml.load_all(open("{}/{}models.yml".format(Const.RESOURCE_PATH, modeltype), 'r')):
            
            counter += 1
            dict['modeltype'] = modeltype
            model = Model(dict)
            models[dict['type']] = model
            
            # Set the default model
            if dict['type'] == 'default':
                defaultmodel = model
            elif 'defaultmodel' not in dict:
                model.set('defaultmodel', defaultmodel)

        print "Loaded {} {} models".format(counter, modeltype)
        return models

    @staticmethod
    def circle(x, y, r):
        blocks = []
        for ix in xrange(int(x-r), int(x+r)):
            for iy in xrange(int(y-r), int(y+r)):