#!/usr/bin/env python

"""This program is a five in row game, which is used for the coding
camp 2015 in WindRiver.com"""

import os
import sys, getopt
import pygame as pg
import threading
import json
from toolbox import button
from toolbox import tools

# Cloud API
from CloudAPI.node import Node
from CloudAPI.config import *

#                 R    G    B
GRAY          = (100, 100, 100)
WHITE         = (255, 255, 255)
RED           = (255,   0,   0)
GREEN         = (  0, 255,   0)
BLUE          = (  0,   0, 255)
YELLOW        = (255, 255,   0)
ORANGE        = (255, 128,   0)
PURPLE        = (255,   0, 255)
CYAN          = (  0, 255, 255)
BLACK         = (  0,   0,   0)
BRIGHT_GREEN  = (  0, 255,   0)
BRIGHT_RED    = (255,   0,   0)
NAVYBLUE      = ( 60,  60, 100)

DRAW = 0
CONTINUE = 1
WIN = 2
ERROR = 3

cloud_service = "Mashery"

def usage():

    USAGE = """\

Usage: %s <-i config_file> [options]

  -i, --ifile=     Input the config file, which contains player user name, 
                   screen width and hight, and input method, etc. 
Options:
  -h, --help       Show this message
  -g, --gameid=    Enter into watching mode and watch the game of [gameid]
                   
Examples:
  %s -i config.json_pc 
  %s -i config.json_touch
  %s -i config.json_watch -g 1
"""
    print (USAGE % ((os.path.basename(__file__),) * 4))


if __name__ == "__main__":

    inputfile = ''
    watch_mode = 0
    watch_game = -1
    try:
        opts, args = getopt.getopt(sys.argv[1:],"hi:g:",["help","ifile=", "gameid="])
    except getopt.GetoptError as err:
        print str(err) 
        usage()
        sys.exit(2)

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt in ("-i", "--ifile="):
            inputfile = arg
        elif opt in ("-g", "--gameid="):
            watch_mode = 1
            try:
                watch_game = int(arg)
            except:
                print "Find latest game to watch", arg

    if inputfile == '':
        usage()
        sys.exit(2)
    else:
        if not os.path.isfile(inputfile):