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

from securetea.lib.malware_analysis.fileAnalysis import FileAnalyser
from securetea.lib.malware_analysis.malwareAnalysis import MalwareAnalysis
from securetea.lib.malware_analysis.continuous_malware_defence import ContinuousDefence
from securetea.lib.malware_analysis import globals
from securetea.lib.malware_analysis.mal_gui import app_runner


class SecureTeaMalwareAnalysis:
    def __init__(self, creds):
        self.mode = creds['mode']
        self.filename = creds['filename']
        self.stegscan = creds['stegscan']
        self.virustotal_api_key = creds['virustotal_api_key']
        globals.initialize_colours()

    def runner(self):

        if self.mode.lower() == 'c':
            GUI = True
            globals.initialize_flask()
            thread1 = ContinuousDefence(gui=GUI, API_KEY=self.api_key)

            # Start new Thread for MalwareAnalysis
            thread1.start()
            if GUI:
                app_runner()

            thread1.join()

        elif self.mode.lower() == 'i':
            f_analysis = FileAnalyser(self.filename)
            action = self.stegscan.lower()
            if action == 'f':
                res = f_analysis.command_file()
                if res is not None:
                    print(res[0])
                    # print(res[1])
                else:
                    print(globals.FAIL + "Error Encountered while running command.\n" + globals.END)
            elif action == 's':
                res = f_analysis.command_strings()
                if res is not None:
                    print(res[0])
                    # print(res[1])
                else:
                    print(globals.FAIL + "Error Encountered while running command.\n" + globals.END)
            elif action == 'e':
                res = f_analysis.command_exiftool()
                if res is not None:
                    print(res[0])
                    # print(res[1])
                else:
                    print("No png Data detected")
            elif action == 'o':
                res = f_analysis.command_foremost()
                if res is not None:
                    print(res[0])
                    # print(res[1])
                else:
                    print(globals.FAIL + "Error Encountered while running command.\n" + globals.END)
            elif action == 'b':
                res = f_analysis.command_binwalk()
                if res is not None:
                    print(res[0])
                    # print(res[1])
                else:
                    print(globals.FAIL + "Error Encountered while running command.\n" + globals.END)
            elif action == 'p':
                res = f_analysis.command_pngcheck()
                if res is not None:
                    print(res[0])
                    # print(res[1])
                else:
                    print("No png Data detected\n")
            elif action == 't':
                res = f_analysis.command_steghide()
                if res is not None:
                    print(res[0])
                    print(res[1])
                else:
                    print(
                        globals.FAIL + "Steghide is incompatible with given files. Use image formats like JPG or JPEG\n" + globals.END)
            elif action == 'u':
                res = f_analysis.command_stegosuite()
                if res is not None:
                    print(res[0])
                    # print(res[1])
                    if 'Wrong' in res[1]:
                        print(globals.FAIL + 'Stegosuite: could not extract any data with that passphrase!' + globals.END)
                else:
                    print(globals.FAIL + "Stegosuite is incompatible with given files. Use image formats like PNG, MNG, JPG or JPEG\n" + globals.END)
            else:
                print(globals.WARNING + "Incorrect choice entered" + globals.END)

        elif self.mode.lower() == 'm':
            m_analysis = MalwareAnalysis(filename=self.filename, API_KEY=self.virustotal_api_key)
            m_analysis.runner()
        else:
            print(globals.WARNING + 'Incorrect option selected' + globals.END)
            return