#!/usr/bin/python3

import os
import cgi
import cgitb
import json
from pprint import pprint
import setup.common as co
import setup.sqlcommon as sqlc
import setup.defaultpaths as dfp
import setup.getinput as gi
import setup.dbsetup as dbs
import setup.gettags as gt
import setup.createsongsjson as csj
import setup.createalbumsjson as calbj
import setup.createartistsjson as cartj
import setup.makealphasoup as aS
import setup.makesoup as ms

class Setup():
	def __init__(self):
		progpath = os.path.dirname(os.path.abspath(__file__))
		p = dfp.SetupPaths()
		jpath = p._get_json_paths(progpath)
		dbpath = p._get_db_paths(progpath)
		other = p._get_other_paths(progpath)
		httpaddr = p._get_http_addr()
		self.progpath = progpath
		self.jpath = jpath
		self.dbpath = dbpath
		self.other = other
		self.httpaddr = httpaddr
		CF = co.CommonFunctions()
		self.CF = CF 
		SQLC = sqlc.SqlCommon()
		self.SQLC = SQLC
		
		GI = gi.GetInputs()
		self.GI = GI

	def _remove_old(self):
		for (p, d, files) in os.walk(self.jpath['programpath'] + "/json"):
			for filename in files:
				fn = os.path.join(p, filename)
				if fn[:-4] == "json":
					os.remove(fn)
		for (p, d, files) in os.walk(self.dbpath['programpath'] + "/db"):
			for filename in files:
				fn = os.path.join(p, filename)
				if os.path.exists(fn):
					


#		for (p, d, files) in os.walk(self.dbpath['programpath'] + "/music"):
#			for filename in files:
#				fn = os.path.join(p, filename)
#				if os.path.exists(fn):
#					os.remove(fn)			
#					
					
					
					
					

	def _get_input_values(self):
		inputs = self.GI._get_inputs()
		return inputs
#		zelda = {}
#		zelda['musicpath']   = "/home/charlie/Desktop/music"
#		zelda['catname']     = "CatalogA"
#		zelda['hostaddress'] = "http://192.168.1.117/ampyche"
#		zelda['dbname']      = "dbname"
#		zelda['dbuname']     = "dbusername"
#		zelda['dbpword']     = "dbpassword"
#		return zelda

	def run_setup(self):		
		self._remove_old()
		z = self._get_input_values()
		cclog = self.CF._create_catalog(z, self.dbpath)
		ampychepath = self.httpaddr + "/" + z['catname']
		print("constants setup")
		
		AA = dbs.AmpycheDBSetup()
		run1 = AA.run(z, self.dbpath)
		print("dbsetup complete")
		
		print("getting tags started")
		AMP = gt.AmpycheGetTags()
		run2 = AMP.run(self.progpath + "/music", self.dbpath, self.other, self.httpaddr, z['catname'], ampychepath)
		print("getTags complete")

		print("start of ID getting")
		aas = self.SQLC._get_artist_album_song_ids(self.dbpath)
		songs = aas[0]
		albums = aas[1]
		artists = aas[2]
		print("get IDS complete")

		CSJ = csj.InitSongsView()
		songs60 = songs[:50]
		writesongsjson = CSJ._get_init_songs_info(self.dbpath, self.jpath, songs60)
		print("CSJ completed")
		
		CALBJ = calbj.InitAlbumView()
		albums40 = albums[:30]
		writealbumjson = CALBJ._get_init_album_info(self.dbpath, self.jpath, albums40)
		print("CALBJ completed")
		
		CARTJ = cartj.InitArtistView()
		artists30 = artists[:30]
		writeartistjson = CARTJ._get_init_artist_info(self.dbpath, self.jpath, artists30)
		print("CARTJ completed")
		
		MAS  = aS.MakeAlphaSoup()
		AlphaSoup = MAS._make_alpha_soup(self.dbpath, self.jpath)

		MS = ms.MakeSoup()
		makesoup = MS.run(self.dbpath, self.jpath)
		
		glip = "SETUP COMPLETE"
		print(json.dumps(glip, sort_keys=True, indent=4))

print("Content-Type: application/json\n\n")	
app = Setup()
Ampyche = app.run_setup()
os.remove(fn)