import os
import sys
import click
from lib import file_helpers

@click.group()
def cli(args=sys.argv):
    """A simple, easy-to-use version control system
    """
    pass

@cli.command("init")
def initialize():
    try:
        os.mkdir(".pvcs")
        os.mkdir(".pvcs/revisions")
    except FileExistsError:
        print("Directory has already been initialized")
        return

    try:
        # Create files for internal use
        open(".pvcs/tracked", "a").close()
        open(".pvcs/staged", "a").close()
        with open(".pvcs/current_rev_number", "a") as f:
            f.write("0")

    # Not sure what exception will be thrown, if any
    # If one is thrown, specific handling can be added for it
    except Exception as e:
        print(repr(e))

    click.echo("Initialized repository")

# TODO: Add support for deleting files
@cli.command("stage")
@click.argument("paths", nargs=-1)
def stage_files(paths):
    files_to_stage = []
    for file_path in paths:

        # Create list of full path for each file listed
        if os.path.isdir(file_path):
            # Loop through directory
            files_to_stage.extend(file_helpers.expand_directory(file_path))
        else:
            # Append instead of extend because extend separates the string into its individual characters
            # This has to do with the way strings act like lists in python
            files_to_stage.append(file_path)

    with open(".pvcs/tracked", "r+") as tracked_files, open(".pvcs/staged", "r+") as staged_files:
        tracked_files_string = tracked_files.read()
        staged_files_string = staged_files.read()

        for file_to_stage in files_to_stage:
            # If the file is not already being tracked, track it
            if staged_files_string.find(file_to_stage) == -1:
                if tracked_files_string.find(file_to_stage) == -1:
                    tracked_files.write(file_to_stage + "\n")
                    staged_files.write("C, " + file_to_stage + "\n")

                else:
                    staged_files.write("D, " + file_to_stage + "\n")

@cli.command("unstage")
@click.argument("paths", nargs=-1)
def unstage_files(paths):
    files_to_unstage = []
    for file_path in paths:

        # Create list of full path for each file listed
        if os.path.isdir(file_path):
            # Loop through directory
            files_to_unstage.extend(file_helpers.expand_directory(file_path))
        else:
            # Append instead of extend because extend separates the string into its individual characters
            # This has to do with the way strings act like lists in python
            files_to_unstage.append(file_path)

    with open(".pvcs/tracked", "r+") as tracked_files, open(".pvcs/staged", "r+") as staged_files:
        for file_to_unstage in files_to_unstage:

            tracked_files_list = tracked_files.readlines()
            staged_files_list = staged_files.readlines()

            tracked_files.seek(0)
            staged_files.seek(0)

            for staged_file in staged_files_list:
                if staged_file.split(",")[1].strip() != file_to_unstage:
                    staged_files.write(staged_file)

                # Untrack the file if it was created in the current commit. Otherwise it should still be tracked
                elif staged_file.split(",")[0].strip() == "C":
                    for tracked_file in tracked_files_list:
                        if tracked_file.strip() != file_to_unstage:
                            tracked_files.write(tracked_file)

            tracked_files.truncate()
            staged_files.truncate()
            tracked_files.seek(0)
            staged_files.seek(0)
    

@cli.command("commit")
@click.option("--message", "-m")
def commit_files(message):
    pass

@cli.command("log")
def show_log():
    pass

@cli.command("revert")
@click.option("--back", "-b", is_flag=True)
@click.argument("number")
def switch_to_commit(back, number):
    pass

@cli.command("status")
def show_status():
    pass

if __name__ == "__main__":
    cli()

@@ -1446,137 +1446,39 @@
 vcs/
-tracked%22, %22r+%22) as tracked_files, open(%22.pvcs/staged%22, %22r+%22) as staged_files:%0A        tracked_files_string = tracked_files.read()
+staged%22, %22r+%22) as staged_files:
 %0A   
@@ -1717,260 +1717,27 @@
     
-if tracked_files_string.find(file_to_stage) == -1:%0A                    tracked_files.write(file_to_stage + %22%5Cn%22)%0A                    staged_files.write(%22C, %22 + file_to_stage + %22%5Cn%22)%0A%0A                else:%0A                    staged_files.write(%22D, %22 + 
+staged_files.write(
 file
@@ -2358,54 +2358,8 @@
 with
- open(%22.pvcs/tracked%22, %22r+%22) as tracked_files,
  ope
@@ -2451,68 +2451,8 @@
 age:
-%0A%0A            tracked_files_list = tracked_files.readlines()
 %0A   
@@ -2508,43 +2508,8 @@
 es()
-%0A%0A            tracked_files.seek(0)
 %0A   
@@ -2731,468 +2731,30 @@
     
-    # Untrack the file if it was created in the current commit. Otherwise it should still be tracked%0A                elif staged_file.split(%22,%22)%5B0%5D.strip() == %22C%22:%0A                    for tracked_file in tracked_files_list:%0A                        if tracked_file.strip() != file_to_unstage:%0A                            tracked_files.write(tracked_file)%0A%0A            tracked_files.truncate()%0A            staged_files.truncate()%0A            tracked_files.seek(0
+staged_files.truncate(
 )%0A  

Change where files are considered tracked