#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import argparse
import logging
import logging.config
import os.path
from datainitializer.ScreenApiFlow import ScreenApiFlow
from datainitializer.ImageString import ImageString
from datainitializer.ApiPipeline import ApiPipeline
from visitor.persist import Persister
from config.constant import Values
from datacollect import parse_dynamic_analysis_results
from packager.behavior import BehaviorProcessor
from gensim.models import KeyedVectors
import re
from datainitializer.AppPipeline import AppPipeline
import operations.scene
import semantic.taint
from visitor.persist import Persister

def get_keywords(descr):
	descr_behaviors = BehaviorProcessor.process(descr)
	keywords = set()
	keywords.update(descr_behaviors.actions)
	keywords.update(descr_behaviors.resources)
	word_filter = operations.scene.Scene().similarity_processor_stopwords
	for res in descr_behaviors.resources:
		if res in word_filter:
			continue
		try:
			similar_res = [x[0] for x in model.wv.most_similar(res,topn=30)]
		except (KeyError):
			similar_res = set()
		keywords.update(similar_res)

	return keywords


def get_multi_ui_layouts(api_flow,is_db):
	refer_str = ''
	pure_json_string = ''
	if api_flow[9]:
		p = re.compile('screen_(.*)\.jpg')
		cur_time = p.search(api_flow[9]).group(1).replace('_','').replace('-','')
		items = is_db.get_multi_json_screenshot_googleimagestr(api_flow[1])
		for item in items:
			if item[4] == 'com.android.launcher2.Launcher':
				continue
			temp_time = p.search(item[0]).group(1).replace('_','').replace('-','')
			if int(cur_time) >= int(temp_time):
				refer_str += '+*-+*-'+item[1] #text class resource_id
				refer_str += '+*-+*-'+item[2] #screenshotstr
				refer_str += '+*-+*-'+item[4] #top activity name
	else:
		refer_str = api_flow[2]
		return refer_str




	return refer_str


def get_ui_refer_keywords(api_flow,is_db):
	if api_flow[9]:
		screen_image_path = os.path.join(api_flow[1],'droidbot/states',api_flow[9])
		json_string,screenshotstr,imageaty = is_db.get_json_screenshot_imageaty(screen_image_path)
		return json_string+'+*-+*-'+screenshotstr+'+*-+*-'+imageaty
	else:
		refer_str = api_flow[2]
		return refer_str


def sig_conversion(signature):
	cls_name = '.'.join(signature.split('.')[:-1])
	mtd_name = signature.split('.')[-1]
	return '@'.join([mtd_name,cls_name])

def handleImageString():
	is_db = ImageString(Values.image_to_string_db)
	is_db.initialize()
	is_db.add_column()
	is_db.finalize()

if __name__ == '__main__':

	logging.config.dictConfig(Values.log_init_config)
	logger = logging.getLogger(__name__)
	# handleImageString()

	# groups = Persister.json_load(Values.all_labeled_tainted_groups_multiIMG_path)
	# exist_id = int(groups[-1]["id"])
	groups = list()

	screen_api_flow_db = ScreenApiFlow(Values.screen_api_flow_db)
	screen_api_flow_db.initialize()
	api_flow_records = screen_api_flow_db.get_flows()
	screen_api_flow_db.finalize()
	
	fdroid_db = AppPipeline(Values.fdroid_28_9_2018_path)
	fdroid_db.initialize()
	
	is_db = ImageString(Values.image_to_string_db)
	is_db.initialize()

	api_desc_db = ApiPipeline(Values.sensitive_api_infos_path)
	api_desc_db.initialize()

	class_description_dicts = Persister.json_load(Values.android_class_description_dict)


	entry_callbacks = set()
	old_index = 0
	indx = 0
	for api_flow in api_flow_records:
		indx+=1
		logger.info(api_flow[0])
		senapi = api_flow[3]
		restrname = None
		if '-' in senapi:
			api_name,restrname = senapi.split('--')
		else:
			api_name = senapi
		cls_name = '.'.join(api_name.split('.')[:-1])
		mtd_name = api_name.split('.')[-1]

		# api_related_text: api_cls_descr, api_mtd_descr
		api_cls_descr = class_description_dicts[cls_name]
		api_mtd_descr= api_desc_db.getapi_summary_descr_perm_by_name(cls_name,mtd_name,restrname)
		
		# flow : entry_callback => api_flow[3]
		entry_callback = parse_dynamic_analysis_results.get_entry_callback(api_flow[5])

		pkgname = api_flow[1].split('/')[-1]
		app_descr = fdroid_db.get_appname_summary_descr_by_pkgname(pkgname)
		app_descr = app_descr.encode('utf-8')

		api_descr = api_cls_descr+api_mtd_descr
		api_descr = api_descr.encode('utf-8')
		

		layout_strings = get_multi_ui_layouts(api_flow,is_db)
		json_string = get_ui_refer_keywords(api_flow,is_db)

		flow = sig_conversion(entry_callback) +'=>'+sig_conversion(api_name)

		group = semantic.taint.Group(idx = str(api_flow[0]), flow=flow, app_descr=app_descr, json_string=json_string,
									 refer_keywords='', api_cls_descr=api_cls_descr.encode('utf-8'), api_mtd_descr=api_mtd_descr.encode('utf-8'), layout_strings = layout_strings, label=api_flow[10])
		groups.append(group.to_json())
	Persister.json_save(groups, Values.all_labeled_tainted_groups_multiIMG_path)
	api_desc_db.finalize()
	is_db.finalize()
	fdroid_db.finalize()
