import csv, json, pandas as pd, glob

def main():
	#print("aaa")
	csvfile="verified.csv"
	df = pd.read_csv(csvfile)
	#for i in range (df.index.stop):
	#	print(i,df.at[i,i])

	#for i in range (df.index.stop):
	#	for col in df.columns.values.tolist():
	#		print(df.at[i][col])

	#print(df)
#	print("[+] Display 2 columns:")
	#display 2 columns:
#	print(df[['Name', 'URL']])

#	print("[+] Display rows 0 to 5")
	#display rows from 0 to 5
#	print(df[0:5])

	#display rows from 0 to 5
#	print("[+] Display rows 3 to 5 and columns Name and URL")
#	print(df.loc[3:5,['Name','URL']])
	#print(df[0:1,['Name','URL']])

#	print("[+] Display all rows for and columns name and URL")
	#df.loc[startrow:endrow, startcolumn:endcolumn]
#	print(df.loc[0:df.index.stop,['Name','URL']])
	
	#display rows from 0 to the end of indexes
#	print("[+] Display all rows for all columns")
#	print(df[0:df.index.stop])




	##############################################################################
	print('\n[+] list the entire csv file and get all row IDs')
	print(df[['Name', 'URL']])
	index=-1
	while (index > df.tail(1).index.item() or index < 0):
		#index=input("\n[+] What entry do you want to edit ?  (enter the row index (ex: 4)")
		index=4
		index=int(index)
	print(df.iloc[index], "last index:", df.index.stop-1)
	print("\n[+] Number of Rows:", len(df.index)-1)



	print('\n[+] Iterate over the rows by their IDs for the 2 Columns URL and Name')
	print(df.iterrows())
	for i,j in df.iterrows():
		#print("[+] ROW=",i,"\n[+] CONTENT=\n",j)
		#print("[+] ROW=",i)
		#print("[+] ROW CONTENTS= \n",df.loc[i, ['URL','Name']])
		#print("[+] ROW CONTENTS= \n",df.loc[i, ['URL']])
		print("[+] ROW=",i,"ROW CONTENTS=",  df.at[i, 'Name'],  df.at[i, 'URL'])
		#print(df[0][i])



	print('\n[+] Iterate over the columns by their name:')
	columns = list(df)
	print(columns)
	print('\n[+] Iterate over the columns of the first row:')
	for i in columns:
		print('\nCOLUMN=',i)
		print('CONTENTS=',df[i][0])
	


	#print('[+] list the csv file by filtering a keyword and get all row IDs')
	#filterterm=input("[+] Filter the CSV file using a keyword (ex: DNM)")
	filterterm="Psy"
	filter_df = df[df.Name.str.contains(filterterm)]
	#print(filtered_df) # print all columns
	print(filter_df[['Name','URL']]) #print only 2 columns
	#print("\n[+] Number of Rows:", len(filter_df.index))
	#for index in filter_df.index:
		#print(index)
	index=-1
	while (index not in filter_df.index):
		#index=int(input("\n[+] Please select a valid row: "))
		index=int("1")
	print("ROW=",index, 'CONTENT=', filter_df.at[index, 'Name'],  filter_df.at[index, 'URL'])



	print("\n[+] Adding a new row:")
	# ask for the following:
	#unverifiedpath=instancepath+'/unverified.csv'
	instance='uptime.nowherejezfoltodf4jiyl6r56jnzintap5vyjlia7fkirfsnfizflqd.onion'
	unverifiedpath='verified.csv'
	# the name of the website (required) + check if its valid
	# if the website name is "exit" then get out of the while loop
	#entry_name = input("What is the Website name ?")
	name="NewWebsite"
	category="TestCategory"
	# the url of the website (required) + check if its valid
	#entry_url = input("What is URL of the Website ? (ex: https://torproject.org or http://2gzyxa5ihm7nsggfxnu52rck2vv4rvmdlkiu3zzui5du4xyclen53wid.onion)")
	url="http://newwebsitewoidwajiawdhjoidwahjoadiwhj.onion"
	# a quick description (optional) + check if its valid
	#entry_desc = input("(Optional) Description of the website ? (max 256 characters) (press enter to skip)")
	desc="This is a new website that we add, it has this description"
	# sensitive ? (y/n) + check if its valid
	#entry_sensi = input("is it a sensitive website ? (ex: website related to drugs) (y/n)")
	sensi = "n"
	newrow=[instance,category,name,url,sensi,desc,'','']
	print("[+] NEWROW=",newrow)

	#add a new row (get all the new data you need first):
		
	df.loc[-1] = newrow  # adding a row
	df.index = df.index + 1  # shifting index
	df = df.sort_index()  # sorting by index
	print("[+] New row added! now writing the csv file:")
	df.to_csv(csvfile, index=False)
	
	print(df)

	#write the dataframe into the csv file
	#read and print the contents of the csv file
	#re-add that row in the csv file
	#remove that row from the dataframe
	print()
		

		# then select a row in it (by the ID) and display it
	# list 
	
if __name__ == '__main__':
	main()
