#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# 2020 Nico Schottelius (nico.schottelius at ungleich.ch)
#
# This file is part of uncloud.
#
# uncloud is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# uncloud is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with uncloud. If not, see <http://www.gnu.org/licenses/>.

# This module is directly called from the hack module, and can be used as follow:
#
#   Create a new VM with default CPU/Memory. The path of the image file is relative to $hackprefix.
#   `uncloud hack --hackprefix /tmp/hackcloud --create-vm --image mysuperimage.qcow2`
#
#   List running VMs (returns a list of UUIDs).
#   `uncloud hack --hackprefix /tmp/hackcloud --list-vms
#
#   Get VM status:
#   `uncloud hack --hackprefix /tmp/hackcloud --get-vm-status --uuid my-vm-uuid`
#
#   Stop a VM:
#   `uncloud hack --hackprefix /tmp/hackcloud --destroy-vm --uuid my-vm-uuid`
#    ``

import subprocess
import uuid
import os
import logging

from uncloud.hack.db import DB
from uncloud.hack.mac import MAC
from uncloud.vmm import VMM
from uncloud.hack.product import Product

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)

class VM(object):
    def __init__(self, config, db_entry=None):
        self.config = config

        #TODO: Enable etcd lookup
        self.no_db = self.config.arguments['no_db']
        if not self.no_db:
             self.db = DB(self.config, prefix="/vm")

        if db_entry:
            self.db_entry = db_entry

        # General CLI arguments.
        self.hackprefix = self.config.arguments['hackprefix']
        self.uuid = self.config.arguments['uuid']
        self.memory = self.config.arguments['memory'] or '1024M'
        self.cores = self.config.arguments['cores'] or 1

        if self.config.arguments['image']:
            self.image = os.path.join(self.hackprefix, self.config.arguments['image'])
        else:
            self.image = None

        if self.config.arguments['image_format']:
            self.image_format=self.config.arguments['image_format']
        else:
            self.image_format='qcow2'

        # External components.

        # This one is broken:
        # TypeError: expected str, bytes or os.PathLike object, not NoneType
        # Fix before re-enabling
        # self.vmm = VMM(vmm_backend=self.hackprefix)
        self.mac = MAC(self.config)

        # Harcoded & generated values.
        self.owner = 'uncloud'
        self.accel = 'kvm'
        self.threads = 1
        self.ifup = os.path.join(self.hackprefix, "ifup.sh")
        self.ifdown = os.path.join(self.hackprefix, "ifdown.sh")
        self.ifname = "uc{}".format(self.mac.to_str_format())

        self.vm = {}

        self.product = Product(config, product_name="dualstack-vm",
                               product_class=self.__class__)
        self.product.define_feature(name="base",
                                    one_time_price=0,
                                    recurring_price=9,
                                    recurring_period="per_month",
                                    minimum_period="per_hour")


        self.features = []


    def get_qemu_args(self):
        command = (
            "-name {owner}-{name}"
            " -machine pc,accel={accel}"
            " -drive file={image},format={image_format},if=virtio"
            " -device virtio-rng-pci"
            " -m {memory} -smp cores={cores},threads={threads}"
            " -netdev tap,id=netmain,script={ifup},downscript={ifdown},ifname={ifname}"
            " -device virtio-net-pci,netdev=netmain,id=net0,mac={mac}"
        ).format(
            owner=self.owner, name=self.uuid,
            accel=self.accel,
            image=self.image, image_format=self.image_format,
            memory=self.memory, cores=self.cores, threads=self.threads,
            ifup=self.ifup, ifdown=self.ifdown, ifname=self.ifname,
            mac=self.mac
        )

        return command.split(" ")

    def create_product(self):
        """Find a VM host and schedule on it"""
        pass

    def create(self):
        # New VM: new UUID, new MAC.
        self.uuid = str(uuid.uuid4())
        self.mac=MAC(self.config)
        self.mac.create()

        qemu_args = self.get_qemu_args()
        log.debug("QEMU args passed to VMM: {}".format(qemu_args))
        self.vmm.start(
                uuid=self.uuid,
                migration=False,
                *qemu_args
                )


        self.mac.create()
        self.vm['mac'] = self.mac
        self.vm['ifname'] = "uc{}".format(self.mac.__repr__())

        # FIXME: TODO: turn this into a string and THEN
        # .split() it later -- easier for using .format()
        #self.vm['commandline'] = [ "{}".format(self.sudo),
        self.vm['commandline'] = "{sudo}{qemu} -name uncloud-{uuid} -machine pc,accel={accel} -m {memory} -smp {cores} -uuid {uuid} -drive file={os_image},media=cdrom -netdev tap,id=netmain,script={ifup},downscript={ifdown},ifname={ifname} -device virtio-net-pci,netdev=netmain,id=net0,mac={mac}"
#        self.vm['commandline'] = [ "{}".format(self.sudo),
#                                    "{}".format(self.qemu),
#                                    "-name", "uncloud-{}".format(self.vm['uuid']),
#                                    "-machine", "pc,accel={}".format(self.accel),
#                                    "-m", "{}".format(self.vm['memory']),
#                                    "-smp", "{}".format(self.vm['cores']),
#                                    "-uuid", "{}".format(self.vm['uuid']),
#                                    "-drive", "file={},media=cdrom".format(self.vm['os_image']),
#                                    "-netdev", "tap,id=netmain,script={},downscript={},ifname={}".format(self.ifup, self.ifdown, self.vm['ifname']),
#                                    "-device", "virtio-net-pci,netdev=netmain,id=net0,mac={}".format(self.vm['mac'])
#        ]

    def _execute_cmd(self, cmd_string, **kwargs):
        cmd = cmd_string.format(**self.vm, **kwargs)
        log.info("Executing: {}".format(cmd))
        subprocess.run(cmd.split())

    def stop(self):
        if not self.uuid:
            print("Please specific an UUID with the --uuid flag.")
            exit(1)

        self.vmm.stop(self.uuid)

    def status(self):
        if not self.uuid:
            print("Please specific an UUID with the --uuid flag.")
            exit(1)

        print(self.vmm.get_status(self.uuid))

    def vnc_addr(self):
        if not self.uuid:
            print("Please specific an UUID with the --uuid flag.")
            exit(1)

        print(self.vmm.get_vnc(self.uuid))

    def list(self):
        print(self.vmm.discover())
