const AWS = require("aws-sdk");
const db = require("./mongo");
const utils = require("./utils");


function _getRgName(provision) {
    let rgName = provision.scenario.toUpperCase();
    rgName = rgName.replace(/AZQMI/g, 'QMI');
    rgName = rgName + "-" + provision._id.toString();
    return rgName;
}

function _getRegion(provision) {
    let region = "eu-west-1";
    if ( provision.deployOpts.location === 'East US') {
        region = "us-east-1";
    } else if ( provision.deployOpts.location === 'Southeast Asia' ) {
        region = "ap-southeast-1";
    }
    return region;
}

async function deallocate(provision, isSendEmailAfter) {
   
    let rgName = _getRgName(provision);
    let region = _getRegion(provision);

    console.log("AWSCLI# Stopping EC2s for resource group: "+rgName);

    var ec2 = new AWS.EC2({apiVersion: '2016-11-15', region: region});
    var params = {
        DryRun: false,
        Filters: [
            {
                Name: 'tag:Name',
                Values: [ `fort-${provision._id}` ]
            },
        ]
    };
    
    ec2.describeInstances(params, async function (err, data) {
        if (err) {
            console.log("AWSCLI# ERROR describing EC2s: "+rgName, err.stack);
        } else {
            
            var ec2Ids = data.Reservations[0].Instances.map(ec2=> ec2.InstanceId);
            console.log("AWSCLI# Stopping EC2s ids", ec2Ids);
            params = {
                DryRun: false,
                InstanceIds: ec2Ids
            };

            ec2.stopInstances(params, async function(err, data) {
                if (err) { 
                    console.log("AWSCLI# ERROR stopping EC2s: "+rgName, err.stack);
                } else  {    
                    console.log("AWSCLI# EC2s stopped!");
                    utils.afterStopVms( provision, isSendEmailAfter );
                }
            });
        }     
    });
}

async function start(provision) {
   
    let rgName = _getRgName(provision);
    let region = _getRegion(provision);

    console.log("AWSCLI# Stopping EC2s for resource group: "+rgName);

    var ec2 = new AWS.EC2({apiVersion: '2016-11-15', region: region});
    var params = {
        DryRun: false,
        Filters: [
            {
                Name: 'tag:Name',
                Values: [ `fort-${provision._id}` ]
            },
        ]
    };
    
    ec2.describeInstances(params, async function (err, data) {
        if (err) {
            console.log("AWSCLI# ERROR describing EC2s: "+rgName, err.stack);
        } else {
            
            var ec2Ids = data.Reservations[0].Instances.map(ec2=> ec2.InstanceId);
            console.log("AWSCLI# Starting EC2s ids", ec2Ids);
            params = {
                DryRun: false,
                InstanceIds: ec2Ids
            };

            ec2.startInstances(params, async function(err, data) {
                if (err) {
                    console.log("AWSCLI# ERROR starting EC2s: "+rgName, err.stack);
                } else if (data) {
                    console.log("AWSCLI# EC2s started!");
                    utils.afterStartVms( provision );
                }
            });
        }     
    });
}

module.exports.deallocate = deallocate;
module.exports.start = start;
