import plistlib
import sys

import biplist
from html_report.artifact_report import ArtifactHtmlReport
from helpers import timeline, tsv

from artifacts.Artifact import Artifact


class SafariRecentWebSearches(ab.Artifact):
    _name = 'Safari Recent WebSearches'
    _search_dirs = '**/Library/Preferences/com.apple.mobilesafari.plist'
    _category = 'Safari Browser'

    def get(self, files_found, seeker):
        data_list = []
        for file_found in files_found:
            file_found = str(file_found)
            with open(file_found, "rb") as fp:
                try:
                    if sys.version_info >= (3, 9):
                        plist = plistlib.load(fp)
                    else:
                        plist = biplist.readPlist(fp)
                    searches = plist.get('RecentWebSearches', [])
                    for search in searches:
                        term = search.get('SearchString', '')
                        date = search.get('Date', '')
                        data_list.append((date, term))
                except (
                    biplist.InvalidPlistException,
                    plistlib.InvalidFileException,
                ) as ex:
                    logfunc(f'Failed to read plist {file_found} ' + str(ex))

        if data_list:
            report = ArtifactHtmlReport('Safari Recent WebSearches')
            report.start_artifact_report(report_folder, 'Recent WebSearches')
            report.add_script()
            data_headers = ('Date', 'Search Term')
            report.write_artifact_data_table(data_headers, data_list, file_found)
            report.end_artifact_report()

            tsvname = 'Recent WebSearches'
            tsv(report_folder, data_headers, data_list, tsvname)
            timeline(report_folder, tsvname, data_list, data_headers)
        else:
            logfunc('No data for recent web searches')
