import Log from "./Log";
import fs from "fs";

export default class Plugin {

    static Type = {
        NORMAL: 0x01,
        INLINE: 0x02,
        PROXY: 0x04,
        SPECIAL: 0x08
    };

    static Visibility = {
        VISIBLE: 0,
        HIDDEN: 1
    };

    static get plugin() {
        return {
            name: "Plugin",
            description: "Base Plugin",
            help: "There is no need to ask for help",
            needs: {
                database: false,
                utils: false
            },

            visibility: Plugin.Visibility.HIDDEN,
            type: Plugin.Type.SPECIAL
        };
    }

    get plugin() {
        return this.constructor.plugin;
    }

    synchronize(cb = err => {
        if (err) throw err;
    }) {
        fs.writeFile(
            `./plugin_${this.plugin.name}.json`,
            JSON.stringify(this.db, null, 4),
            cb
        );
    }

    constructor(listener, bot) {
        if (new.target === Plugin) {
            throw new TypeError("Cannot construct Plugin instances directly!");
        }

        this.log = Log.get(this.plugin.name);
        this.listener = listener;
        this.bot = bot;

        if (this.plugin.needs) {
            if (this.plugin.needs.database) {
                fs.readFile(`./plugin_${this.plugin.name}.json`, "utf8", (err, val) => {
                    if (err) {
                        console.log(err);
                        this.db = {};
                    } else {
                        this.db = JSON.parse(val);
                    }
                });
            }
        }

        this.syncInterval = 5000;

        setInterval(() => this.synchronize(), this.syncInterval);

        // text
        if (typeof this.onText === 'function')
            this.listener.on("text", (...args) => this.onText(...args));

        // media
        if (typeof this.onAudio === 'function')
            this.listener.on("audio", (...args) => this.onAudio(...args));
        if (typeof this.onDocument === 'function')
            this.listener.on("document", (...args) => this.onDocument(...args));
        if (typeof this.onPhoto === 'function')
            this.listener.on("photo", (...args) => this.onPhoto(...args));
        if (typeof this.onSticker === 'function')
            this.listener.on("sticker", (...args) => this.onSticker(...args));
        if (typeof this.onVideo === 'function')
            this.listener.on("video", (...args) => this.onVideo(...args));
        if (typeof this.onVoice === 'function')
            this.listener.on("voice", (...args) => this.onVoice(...args));

        // other
        if (typeof this.onContact === 'function')
            this.listener.on("contact", (...args) => this.onContact(...args));
        if (typeof this.onLocation === 'function')
            this.listener.on("location", (...args) => this.onLocation(...args));
        if (typeof this.onNewChatParticipant === 'function')
            this.listener.on("new_chat_participant", (...args) => this.onNewChatParticipant(...args));
        if (typeof this.onLeftChatParticipant === 'function')
            this.listener.on("left_chat_participant", (...args) => this.onLeftChatParticipant(...args));
    }

    start() {
        return;
    }

    stop() {
        return;
    }
}

@@ -1267,24 +1267,95 @@
             
+ let serialized;%0A                try %7B%0A                    serialized =
  fs.readFile
@@ -1354,16 +1354,20 @@
 readFile
+Sync
 (%60./plug
@@ -1406,25 +1406,10 @@
 tf8%22
-, (err, val) =%3E %7B
+);
 %0A   
@@ -1421,27 +1421,26 @@
         
-    if
+%7D catch
  (e
-rr
 ) %7B%0A    
@@ -1459,66 +1459,25 @@
     
-    console.log(err);%0A                        this.db
+serialized
  = 
+%22
 %7B%7D
+%22
 ;%0A  
@@ -1494,23 +1494,10 @@
     
-    %7D else %7B%0A  
+%7D%0A
     
@@ -1504,30 +1504,24 @@
             
-      
 this.db = JS
@@ -1533,53 +1533,18 @@
 rse(
-val);%0A                    %7D%0A                %7D
+serialized
 );%0A 

Make initial deserialization synchronous