#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      14JHarvey
#
# Created:     25/04/2018
# Copyright:   (c) 14JHarvey 2018
# Licence:     <your licence>
#-------------------------------------------------------------------------------
try:
    import os
    import sys
    import time
    import json

    from os import system
    from sys import stdout
    from time import sleep
except ImportError as e:
    print(str(e))


Name = ["james", "cf64 4jr","ty", "cf64 3lz"]
##try:
##  f = open("Name.txt", "r")
##  Name = json.loads(f.read())
##  f.close()
##except:
##  pass
name = ""
position = 0
loyalty_card_number = ""
loyalty_list = []
loyalty = ""
Name1 = ""
Postcode1 = ""
save = False
def new():
    global Name, Postcode1, Name1
    sleep(0.5)
    save = False
    while True:
        while True:
            Name1 = input("NAME: ")
            Name1 = Name1.lower()
            sleep(0.78)
            Postcode1 = input("POSTCODE: ")
            Postcode1 = Postcode1.lower()
            Postcode2 = list(Postcode1)
            print(Postcode2)
##            Name1 = list(Name1)
##            if " " in Postcode1:
##                Postcode1 = Postcode1.remove(" ")
##                if " " in Postcode1:
##                    Postcode1 = Postcode1.remove(" ") #still
##            Postcode1 = ''.join(str(e) for e in Postcode1)
##            if " " in Name1:
##                Name1 = Name1.remove(" ")
##            Name1 = ''.join(str(e) for e in Postcode1)
##            if Name:
##                Name = Name.append(Name1)
##                save = True
##            else:
##                break
##            if Name1:
##                Name = Name.append(Postcode1)
    if save:
      try:
        f = open("Name.txt", "w")
        f.write(json.dumps(Name, indent=2))
        f.close()
      except:
        print("failed to write file")
    sleep(1)
    begin()
def end():
    cost = input("Cost of item: ")
    new_cost = (int(cost)/10)
    print("New cost: ", new_cost)
    sleep(0.78)
    begin()
def check():
    global loyalty_card_number, loyalty_list, loyalty
    seven = loyalty[7]
    loyalty = list(loyalty_card_number)
    check_digit = str(seven)
    loyalty.remove(seven)
    loyalty = (loyalty[::-1])
    one = (int(loyalty[0])*2) #2
    three = (int(loyalty[2])*2) #6
    five = (int(loyalty[4])*2) #1
    seven = (int(loyalty[6])*2) #5
    if one > 9:
        one = (int(one - 9))
    if three > 9:
        two = (int(one - 9))
    if five > 9:
        five = (int(five - 9))
    if seven > 9:
        seven = (int(seven - 9))
    numbers = (one + (int(loyalty[1]) + three + int(loyalty[3]) + five + int(loyalty[5]) + seven + (int(check_digit))))
    numbers = (int(numbers))
    if (numbers % 10) == 0:
        system("color a")
        print("Loyalty card is valid")
        sleep(0.56)
    else:
        system("color c")
        print("Loyalty card number not valid")
        begin()
    end()
def existing():
    global loyalty_card_number, loyalty_list, loyalty
    sleep(0.57)
    name = input("NAME: ")
    name = name.lower()
    if name in Name:
        position = Name.index(name)
        position = (int(position))
        POSTCODE = (int(position + 1))
        LOYALTY_CARD_NUMBER = POSTCODE
    else:
        system("color c")
        print("Sorry that name is not registered within this systen")
        sleep(2)
        begin()
    postcode = input("POSTCODE: ")
    postcode = postcode.lower()
    if postcode == Name[POSTCODE]:
        print("VERIFIED")
        sleep(1)
        pass
    else:
        print("POSTCODE DOES NOT MATCH ENTERED NAME")
        sleep(2)
        begin()
    loyalty_card_number = input("LOYALTY CARD NUMBER:")
    loyalty = loyalty_card_number
    loyalty_card_number = list(loyalty_card_number)
    if " " in loyalty_card_number:
        loyalty_card_number.remove(" ")
    else:
        pass
    loyalty_card_number = ''.join(str(e) for e in loyalty_card_number)
    if len(loyalty_card_number) == 8:
        check()
    else:
        sleep(0.5)
        print("LOYALTY CARD NUMBER IS INCORRECT")
        begin()
def begin():
    name = ""
    position = 0
    loyalty_card_number = ""
    loyalty_list = []
    loyalty = ""
    Name1 = ""
    Postcode1 = ""
    save = False
    system("color a")
    create_or_exist = input("Log into existing account [1]? or Register a new one [2]? ")
    if create_or_exist == "1":
        existing()
    elif create_or_exist == "2":
        new()
##if KeyboardInterrupt:
##    exit1 = input("Are You Sure You Want To Exit?: Y/N")
##    exit1.lower()
##    if exit1 == "y":
##        exit()
##    elif exit1 == "n":
##        sleep(0.75)
##        begin()
##    else:
##        sleep(0.62)
##        begin()
begin()
