# -*- coding: utf-8 -*-


import sqlite3
import os
import AppItem


class AppPipeline(object):
	db_filename = ''
	conn = None

	def __init__(self, DB_NAME):
		self.db_filename = DB_NAME
		print '# DB name: ' + DB_NAME
		self.conn = None

	def insert_into_DB(self, item):

		one = self.conn.execute('select * from appinfo where apkname = ?', (item.apkname,)).fetchone()
		# if this app info is not in database, then insert it, or just skip
		if one is None:
			try:
				self.conn.execute('insert into appinfo(apppath, apkname, pkgname, mainaty, permissions, appname,summary,description) values(?, ?, ?, ?, ?, ?, ?, ?)',
			 					(item.apppath, item.apkname, item.pkgname, item.mainaty, item.permissions, item.appname,item.summary,item.description))
			except sqlite3.IntegrityError:
				print 'Integrity Error'
		else:
			self.update_row(item)
		self.conn.commit()

	def process_item(self, item):
		self.insert_into_DB(item)
		return item
	
	def initialize(self):
		print '# initialize: ' + self.db_filename
		if os.path.exists(self.db_filename):
			self.conn = sqlite3.connect(self.db_filename)
		else:
			print '####### no db'
			self.create_table()

		self.conn.commit()

	def finalize(self):
		if self.conn is not None:
			self.conn.commit()
			self.conn.close()
			self.conn = None

	# create tables if database file does not exist
	def create_table(self):
		print '#### Crate database'
		self.conn = sqlite3.connect(self.db_filename)
		self.conn.execute("create table appinfo ( \
						id integer not null primary key autoincrement unique, \
						apppath int not null, \
						apkname text not null, \
						pkgname text not null, \
						mainaty text not null, \
						permissions text, \
						appname text, \
						summary text, \
						description text \
						)")
		self.conn.commit()

	def update_row(self, item):
		self.conn.execute('update appinfo set apppath = ?, pkgname = ?, mainaty = ?, permissions = ?, appname = ?, summary = ?, description = ? where apkname = ?',
			 (item.apppath, item.pkgname, item.mainaty, item.permissions, item.appname,item.summary,item.description, item.apkname,))
		self.conn.commit()

	def update_name_summary_desc(self, app_name,app_summary,app_description, apkname):
		self.conn.execute('update appinfo set appname = ?, summary = ?, description = ? where apkname = ?', (app_name,app_summary,app_description, apkname,))
		self.conn.commit()

	def update_pkgname_mainaty_permissions(self, apppath,pkgname,mainaty, permissions):
		self.conn.execute('update appinfo set pkgname = ?, mainaty = ?, permissions = ? where apppath = ?', (pkgname,mainaty, permissions, apppath,))
		self.conn.commit()

	def get_all_apps(self):
		apks = []
		cursor = self.conn.execute('select apkname,pkgname,appname from appinfo')
		self.conn.commit()
		for row in cursor:
			apks.append(row)
		return apks
	
	def get_all_apps_apppath_pkgname_mainaty_perms(self):
		apks = []
		cursor = self.conn.execute('select apppath,pkgname,mainaty,permissions from appinfo')
		self.conn.commit()
		for row in cursor:
			apks.append(row)
		return apks

	def get_all_apps_apppath_apkname_pkgname_mainaty_perms(self):
		apks = []
		cursor = self.conn.execute('select apppath,apkname,pkgname,mainaty,permissions from appinfo')
		self.conn.commit()
		for row in cursor:
			apks.append(row)
		return apks

	def get_appname_summary_descr_by_pkgname(self,pkgname):
		cursor = self.conn.execute('select appname,summary,description from appinfo where pkgname = ?',(pkgname,)).fetchone()
		self.conn.commit()
		if cursor is None:
			return ''
		str_list = list(filter(None, cursor))
			
		return ' '.join(str_list)