import { usePrivateKeyHash } from "../providers/AccountProvider/usePrivateKeyHash";
import { usePrivatePlain } from "../providers/AccountProvider/usePrivatePlain";
import { usePublicKey } from "../providers/AccountProvider/usePublicKey";

export enum ConsoleLogLevel {
    INFO = "INFO",
    ERROR = "ERROR",
    WARN = "WARN"
}

export function useConsoleLogger(component: string) {
    const privatePlain = usePrivatePlain();
    const publicKey = usePublicKey();
    const privateKey = usePrivateKeyHash();

    const constructTechnicalDetails = () => {
        let time = new Date().toISOString();
        //console.groupCollapsed('Details');
            console.log('%cPublic Key: %c%s', 'color: orange; font-weight: bold;', 'color: white;', publicKey.trim() != "" ? publicKey : '[EMPTY]');
            console.log('%cPrivate Key Hash: %c%s', 'color: orange; font-weight: bold;', 'color: white;', privateKey.trim() != "" ? privateKey : '[EMPTY]');
            console.log('%cPrivate Plain: %c%s', 'color: orange; font-weight: bold;', 'color: white;', privatePlain.trim() != "" ? privatePlain : '[EMPTY]');
        //console.groupEnd();
        //console.groupCollapsed('Trace');
            console.info('%cTime: %c%s', 'color: green; font-weight: bold;', 'color: white;', time);
            console.info('%cComponent:%c %s', 'color: red; font-weight: bold;', 'color: #7CFC00; font-weight: bold;', component);
            console.trace('%cStack trace:', 'color: purple; font-weight: bold;');
        //console.groupEnd();
    }

    const constructLogLevelColor = (logLevel : ConsoleLogLevel) => {
        switch(logLevel){
            case ConsoleLogLevel.INFO:
                return 'color: white; background-color: #00B5FF;';
            case ConsoleLogLevel.ERROR:
                return 'color: white; background-color: red;';
            case ConsoleLogLevel.WARN:
                return 'color: black; background-color: yellow;';
            default:
                return 'color: black; background-color: white;';
        }
    }

    const constructMessageDetails = (message: string, logLevel : ConsoleLogLevel) => {
        return [
            `%c ${logLevel} %c ${message}`,
            constructLogLevelColor(logLevel),
            'color: #fefefe; font-weight: normal;'
        ];
    }
    
    const error = (message : string) => {
        console.groupCollapsed(...constructMessageDetails(message, ConsoleLogLevel.ERROR));
        constructTechnicalDetails();
        console.groupEnd();
    }

    const info = (message : string) => {
        console.groupCollapsed(...constructMessageDetails(message, ConsoleLogLevel.INFO));
        constructTechnicalDetails();
        console.groupEnd();
    }

    const warn = (message : string) => {
        console.groupCollapsed(...constructMessageDetails(message, ConsoleLogLevel.WARN));
        constructTechnicalDetails();
        console.groupEnd();        
    }

    return {error, info, warn};
}