import sys
import string

from spreadsheetsubject import *

from plotobserver import *
from spreadsheetobserver import *

class TxtInterface:
    def __init__(self):
	self.subject=SpreadSheetSubject()
	        
    def run(self):
	
        while 1:
            cmd=raw_input(">>> ")

            if cmd=="":
                continue

            cmdargs=string.split(cmd)

	    if cmdargs[0]=="quit":
		break;
	    if cmdargs[0]=="plot":
                if not len(cmdargs)==5:
                    print "Four args needed to specify the cell range"
                else:
                    try:
                        self._user_PlotObserver(string.atoi(cmdargs[1]), string.atoi(cmdargs[2]),\
				   string.atoi(cmdargs[3]), string.atoi(cmdargs[4]))
                    except ValueError:
                        print "The position must be integer"
	    if cmdargs[0]=="set":
                if not len(cmdargs)==4:
                    print "Three args needed to change a value of a cell"
                else:
                    try:
                        #data=Data(string.atoi(cmdargs[1]), string.atoi(cmdargs[2]), \
			#		string.atof(cmdargs[3]))
                        #self.subject.setState(data)
                        self.subject.setState(string.atoi(cmdargs[1]),string.atoi(cmdargs[2]),\
                                              cmdargs[3])                           
                    except ValueError:
                        print "The cell position must be integer, and its value must be numeric"
	
	    if cmdargs[0]=="spreadsheet":
                if not len(cmdargs)==5:
                    print "Four args needed to specify the cell range"
                else:
                    try:
                        self._user_SpreadsheetObserver(string.atoi(cmdargs[1]), \
				string.atoi(cmdargs[2]), string.atoi(cmdargs[3]), \
			        string.atoi(cmdargs[4]))
                    except ValueError:
                        print "The cell position must be integer"

    def _user_PlotObserver(self, startx, starty, endx, endy):
        PlotObserver(self.subject, (startx, starty), (endx, endy))

    def _user_SpreadsheetObserver(self, startx, starty, endx, endy):
        tk=Tk()
        SpreadsheetObserver(self.subject, startx, starty, endx, endy, master=tk)

if __name__ == '__main__':
	TxtInterface().run()
