# !/bin/python
# -*- coding: utf-8 -*-
u"""SecureTea Social Engineering
Project:
    ╔═╗┌─┐┌─┐┬ ┬┬─┐┌─┐╔╦╗┌─┐┌─┐
    ╚═╗├┤ │  │ │├┬┘├┤  ║ ├┤ ├─┤
    ╚═╝└─┘└─┘└─┘┴└─└─┘ ╩ └─┘┴ ┴
    Author: Digvijay Bhosale <digvijayb1729@gmail.com> August 16 2021
    Version: 1.0
    Module: SecureTea
"""

import json
import pandas
from securetea.lib.malware_analysis import globals

class JSONDisplay:
    '''
    Contains functions that analyse JSON responses from VirusTotal API and displays formatted results

    verbosity_1()
    Displays how many Antivirus engines detected malware in selected file

    verbosity_2()
    Displays Antiviruses along with their detection statuses in a formatted manner

    verbosity_3()
    Displays Antiviruses along with their detection statuses, method and result in a formatted manner

    verbosity_4()
    Display all information about scan in a table format. Uses pandas to create dataframe from JSON and then display it out on screen
    '''

    def __init__(self, av_engines_list):
        pass
        self.results_df = pandas.DataFrame(av_engines_list)
        self.results_df = self.results_df.transpose()
        globals.initialize_colours()

    def threat_level_threading(self, filename, report_id):
        """
        takes JSON responses from VirusTotal API
        Edits 5 global dicts from globals.py
        Puts values like - filename : value
        No need to return anything because Global vars
        """
        detected = 0
        undetected = 0
        unsupported = 0
        total = 0

        av_names = self.results_df.index

        for av_name in av_names:
            status = self.results_df.loc[av_name]['category']
            if status == 'undetected':
                undetected = undetected + 1
                total = total + 1
            elif status == 'malicious':
                detected = detected + 1
                total = total + 1
            elif status == 'type-unsupported':
                unsupported = unsupported + 1
                total = total + 1
            else:
                total = total + 1

        globals.detected[filename] = detected
        globals.undetected[filename] = undetected
        globals.unsupported[filename] = unsupported
        globals.total[filename] = total
        globals.report_id[filename] = report_id

    def threat_level(self):
        undetected = 0
        detected = 0
        unsupported = 0
        total = 0
        av_names = self.results_df.index
        for av_name in av_names:
            status = self.results_df.loc[av_name]['category']
            print(status)
            if status == 'undetected':
                undetected = undetected + 1
                total = total + 1
            elif status == 'detected':
                detected = detected + 1
                total = total + 1
            elif status == 'type-unsupported':
                unsupported = unsupported + 1
                total = total + 1
            else:
                total = total + 1

        return detected, undetected, unsupported, total

    def verbosity_1(self):
        undetected = 0
        detected = 0
        unsupported = 0
        total = 0
        av_names = self.results_df.index
        for av_name in av_names:
            status = self.results_df.loc[av_name]['category']
            if status == 'undetected':
                undetected = undetected + 1
                total = total + 1
            elif status == 'detected':
                detected = detected + 1
                total = total + 1
            elif status == 'type-unsupported':
                unsupported = unsupported + 1
                total = total + 1
            else:
                total = total + 1

        print(globals.RED + 'Detected : ' + str(detected) + '/' + str(total) + globals.END)
        print('Undetected : ' + str(undetected) + '/' + str(total))
        print('Type unsupported : ' + str(unsupported) + '/' + str(total))

    def verbosity_2(self):
        print((self.results_df.loc[:, ['category']]).to_string())

    def verbosity_3(self):
        print((self.results_df.loc[:, ['category', 'result', 'method']]).to_string())

    def verbosity_4(self):
        print(self.results_df.to_string())
