#! /usr/bin/env python

import sys
import os
import pickle
import subprocess

#######################
#
# File:         cheatsheet.py
# Author:       Bodsda - bodsda@ubuntu.com
# Licene:       GPLv3 
#
#######################


class cliArgs:
    def __init__(self):
        self.args = ["--use", "--store", "--remove", "--list", "--find", "--debug", "--help", "-u", "-s", "-r", "-l", "-f", "-d", "-h"]
        self.listArgUse = ["--use", "-u"]
        self.listArgStore = ["--store", "-s"]
        self.listArgRemove = ["--remove", "-r"]
        self.listArgList = ["--list", "-l"]
        self.listArgFind = ["--find", "-f"]
        self.listArgDebug = ["--debug", "-d"]
        self.listArgHelp = ["--help", "-h"]

    def parseArgs(self):
        print "here?"
        arg = sys.argv[1]
        
        if arg in self.listArgStore:
            self.argStore()
            
        elif arg in self.listArgRemove:
            self.argRemove()
            
        elif arg in self.listArgList:
            self.argList()
            
        elif arg in self.listArgFind:
            self.argFind()
            
        elif arg in self.listArgDebug:
            self.argDebug()
            
        else:
            self.argHelp()                        

    def listArgs(self, argtype = "all"):
        if argytype == "all":
            for arg in self.args:
                print arg

        elif argtype == "single":
            for arg in self.args:
                if len(arg) == 2:
                    print arg

        elif argtype == "double":
            for arg in self.args:
                if arg.startswith("--"):
                    print arg

    def argUse(self):
        pass
        
    def argStore(self):
        command = sys.argv[2]
        key = sys.argv[3]
        
        handle = cf.load()
        
        for index in handle.iterkeys():
            pass
        index += 1
        
        handle[index] = [command, key]
        
        cf.save(handle)
        
        print "Index: %s, Stored: %s, with key: %s" % (index, command, key)

    def argRemove(self):
        index = sys.argv[2] 
       
        index = int(index)
        
        handle = cf.load()
    
        handle.pop(index)
    
        cf.save(handle)

    def argList(self):
        handle = cf.load()
        
        for index in handle.iterkeys():
            print "Index: %s, Command: %s, Key: %s" % (index, handle[index][0], handle[index][1])

    def argFind(self):
        handle = cf.load()
        
        try:
            key = int(sys.argv[2])
            print handle[key]
            
        except ValueError:
            key = sys.argv[2]
            
            for item in handle.iterkeys():
                if handle[item][1] == key:
                    print handle[item][0]            

    def argDebug(self):
        count = 0
        for item in sys.argv:
            print "sys.argv[%s]: %s" % (count, sys.argv[count])
            count += 1
            
    def argHelp(self):
        pass
            
        
class cheatFile:
    def __init__(self):    
        if os.name == "nt":
            self.cheatfile = "C:\\.cheatsheet"
            
            if not os.path.isfile(self.cheatfile):
                print "Creating cheatfile..."
                file = open(self.cheatfile, "w")
                print "Cheatfile created."
                file.close()
            
                subprocess.Popen("attrib.exe +H %s" % self.cheatfile)
                
        else:
            self.cheatfile = "~/.cheatfile"
            
            if not os.path.isfile(os.path.expanduser(self.cheatfile)):
                print "Creating cheatfile..."
                file = open(self.cheatfile, "w")
                print "Cheatfile created."
                file.close()
                
    def load(self):
        return pickle.load(self.cheatfile)
        
    def save(self, handle):
        pickle.dump(handle, self.cheatfile)
        
    def clear(self):
        self.save("")
        
    def remove(self, handle, index):
        handle.pop(index)
        self.save(handle)
        
def main():
    cf = cheatFile()
    print "done"
    ca = cliArgs()
    
    ca.parseArgs()
    
main()