import { Component, OnInit } from "@angular/core";
import { Server } from '../../../../../models/server';
import { ActivatedRoute, Router } from '@angular/router';
import { ServerService } from '../../../../../services/server.service';
import { ToasterService } from '../../../../../services/toaster.service';
import { v4 as uuid } from 'uuid';
import { TemplateMocksService } from '../../../../../services/template-mocks.service';
import { BuiltInTemplatesService } from '../../../../../services/built-in-templates.service';
import { EthernetHubTemplate } from '../../../../../models/templates/ethernet-hub-template';
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';
import { ComputeService } from '../../../../../services/compute.service';
import { Compute } from '../../../../../models/compute';


@Component({
    selector: 'app-ethernet-hubs-add-template',
    templateUrl: './ethernet-hubs-add-template.component.html',
    styleUrls: ['./ethernet-hubs-add-template.component.scss', '../../../preferences.component.scss']
})
export class EthernetHubsAddTemplateComponent implements OnInit {
    server: Server;
    templateName: string = '';
    formGroup: FormGroup;

    isGns3VmAvailable: boolean = false;
    isGns3VmChosen: boolean = false;
    isLocalComputerChosen: boolean = true;
    
    constructor(
        private route: ActivatedRoute,
        private serverService: ServerService,
        private builtInTemplatesService: BuiltInTemplatesService,
        private router: Router,
        private toasterService: ToasterService,
        private templateMocksService: TemplateMocksService,
        private formBuilder: FormBuilder,
        private computeService: ComputeService
    ) {
        this.formGroup = this.formBuilder.group({
            templateName: new FormControl('', Validators.required),
            numberOfPorts: new FormControl(8, Validators.required)
        });
    }

    ngOnInit() {
        const server_id = this.route.snapshot.paramMap.get("server_id");
        this.serverService.get(parseInt(server_id, 10)).then((server: Server) => {
            this.server = server;

            this.computeService.getComputes(server).subscribe((computes: Compute[]) => {
                if (computes.filter(compute => compute.compute_id === 'vm').length > 0) this.isGns3VmAvailable = true;
            });
        });
    }

    setServerType(serverType: string) {
        if (serverType === 'gns3 vm' && this.isGns3VmAvailable) {
            this.isGns3VmChosen = true;
            this.isLocalComputerChosen = false;
        } else {
            this.isGns3VmChosen = false;
            this.isLocalComputerChosen = true;
        }
    }

    goBack() {
        this.router.navigate(['/server', this.server.id, 'preferences', 'builtin', 'ethernet-hubs']);
    }

    addTemplate() {
        if (!this.formGroup.invalid) {
            let ethernetHubTemplate: EthernetHubTemplate;

            this.templateMocksService.getEthernetHubTemplate().subscribe((template: EthernetHubTemplate) => {
                ethernetHubTemplate = template;
            });

            ethernetHubTemplate.template_id = uuid();
            ethernetHubTemplate.name = this.formGroup.get('templateName').value;
            ethernetHubTemplate.compute_id = this.isGns3VmChosen ? 'vm' : 'local';

            for(let i=0; i<this.formGroup.get('numberOfPorts').value; i++){
                ethernetHubTemplate.ports_mapping.push({
                    name: `Ethernet${i}`,
                    port_number: i
                });
            }

            this.builtInTemplatesService.addTemplate(this.server, ethernetHubTemplate).subscribe(() => {
                this.goBack();
            });
        } else {
            this.toasterService.error(`Fill all required fields`);
        }
    }
}
