const { LogType } = require("loguix")
const fs = require("fs")
const path = require("path")
const { __glob } = require("./global-variables")
const clog = new LogType("Services")

const http = require('http');
const https = require('https');

module.exports.getServiceStatus = function(service) {
    const protocol = service.startsWith('https') ? https : http;
    const url = new URL(service);
    const options = {
        method: 'HEAD',
        host: url.hostname,
        port: url.port,
        path: url.pathname,
    };

    return new Promise((resolve, reject) => {
        const req = protocol.request(options, (res) => {
            if(res.statusCode !== 502) {
                resolve("ONLINE");
            } else {

                resolve("OFFLINE");
            }
        
        });

        req.on('error', (err) => {
            resolve("OFFLINE");
        });

        req.end();
    });
}

module.exports.stopService = function(service) {
    
    
    return new Promise((resolve, reject) => {
       
        const child_process = require('child_process');
        if(service == `https://subsonics.raphix.fr`) {
            let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@alpha.raphix.fr sudo -S -u gitlab-ci 'pm2 stop "Subsonics"'`)
            resolve("OK")
        } else if(service == `https://git.raphix.fr` ) {
            let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@omega.raphix.fr sudo -S systemctl stop gitea`)
            resolve("OK")
        } else if(service == `https://jenkins.raphix.fr`) {
            let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@omega.raphix.fr sudo -S systemctl stop jenkins `)
            resolve("OK")

        } else if(service == `https://raphix.fr`) {
            let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@alpha.raphix.fr sudo -S -u gitlab-ci 'pm2 stop "Website - Raphix"'`)
            resolve("OK")
        } else if(service == `https://cv.raphix.fr`) {
            let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@alpha.raphix.fr sudo -S -u gitlab-ci 'pm2 stop "CV - Raphael"'`)
            resolve("OK")
        } else if(service == `http://omega.raphix.fr:2333`) {
            let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@omega.raphix.fr sudo -S systemctl stop lavalink `)
            resolve("OK")
        } else {
            resolve(false)
        }

    });
    
}

module.exports.startService = function(service) {
    
    
    return new Promise((resolve, reject) => {
       
        const child_process = require('child_process');
        if(service == `https://subsonics.raphix.fr`) {
            let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@alpha.raphix.fr sudo -S -u gitlab-ci 'pm2 start /home/gitlab-ci/subsonic.config.js'`)
            resolve("OK")
           
        } else if(service == `https://git.raphix.fr` ) {
            let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@omega.raphix.fr sudo -S systemctl start gitea`)
            resolve("OK")
        } else if(service == `https://jenkins.raphix.fr`) {
            let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@omega.raphix.fr sudo -S systemctl start jenkins `)
            resolve("OK")

        } else if(service == `https://raphix.fr`) {
            let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@alpha.raphix.fr sudo -S -u gitlab-ci 'pm2 start /home/gitlab-ci/website.config.js'`)
            resolve("OK")
        } else if(service == `https://cv.raphix.fr`) {
            let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@alpha.raphix.fr sudo -S -u gitlab-ci 'pm2 start /home/gitlab-ci/cv.config.js'`)
            resolve("OK")
        } else if(service == `http://omega.raphix.fr:2333`) {
            let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@omega.raphix.fr sudo -S systemctl start lavalink `)
            resolve("OK")
        } else {
            resolve(false)
        }

    });
    
}

module.exports.restartService = function(service) {
    
    
    return new Promise((resolve, reject) => {
       
        const child_process = require('child_process');
        if(service == `https://subsonics.raphix.fr`) {
            let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@alpha.raphix.fr sudo -S -u gitlab-ci 'pm2 restart "Subsonics"'`)
            resolve("OK")
        } else if(service == `https://git.raphix.fr` ) {
            let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@omega.raphix.fr sudo -S systemctl restart gitea`)
            resolve("OK")
        } else if(service == `https://jenkins.raphix.fr`) {
            let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@omega.raphix.fr sudo -S systemctl restart jenkins `)
            resolve("OK")

        } else if(service == `https://raphix.fr`) {
            let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@alpha.raphix.fr sudo -S -u gitlab-ci 'pm2 restart "Website - Raphix"'`)
            resolve("OK")
        } else if(service == `https://cv.raphix.fr`) {
            let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@alpha.raphix.fr sudo -S -u gitlab-ci 'pm2 restart "CV - Raphael"'`)
            resolve("OK")
        } else if(service == `http://omega.raphix.fr:2333`) {
            let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@omega.raphix.fr sudo -S systemctl restart lavalink `)
            resolve("OK")
        } else {
            resolve(false)
        }

    });
    
}