"""Qemu commands to run and handle the image"""
import os
import sys
import argparse
from subprocess import Popen, PIPE

from gentooimgr import LOG
import gentooimgr.config
import gentooimgr.common
def create_image(args, config: dict, overwrite: bool = False) -> str:
    """Creates an image (.img) file using qemu that will be used to create the cloud image

    :Parameters:
        - config: dictionary/json configuration containing required information
        - overwrite: if True, run_image() will call this and re-create.


    :Returns:
        Full path to image file produced by qemu
    """

    image = gentooimgr.common.get_image_name(args, config)
    name, ext = os.path.splitext(image)
    if os.path.exists(image) and not overwrite:
        return os.path.abspath(image)

    cmd = ['qemu-img', 'create', '-f', ext[1:], image, str(config.get("imgsize", "12G"))]
    proc = Popen(cmd, stderr=PIPE, stdout=PIPE)
    stdout, stderr = proc.communicate()
    return os.path.abspath(image)

def run_image(
    args: argparse.Namespace,
    config: dict,
    mounts=[]):
    """Handle mounts and run the live cd image

        - mount_isos: list of iso paths to mount in qemu as disks.
    """
    iso = config.get("iso")
    prefix = args.temporary_dir
    LOG.info(f"iso from config {iso}")
    if iso is None:
        iso = gentooimgr.common.find_iso(
            os.path.join(
                os.path.abspath(os.path.dirname(__file__)),
                ".."
            )
        )
        LOG.info(f"iso from cwd {iso}")
    if not iso:
        prefix = config.get('temporary_dir')
        iso = gentooimgr.common.find_iso(prefix)
        LOG.info(f"iso from {prefix} {iso}")

    assert iso, f"iso not found {iso}"

    if isinstance(iso, list):
        assert len(iso), f"iso list is empty {iso}"
        iso = iso[0]

    image = gentooimgr.common.get_image_name(args, config)
    qmounts = []
    mounts.extend(args.mounts)
    for i in mounts:
        qmounts.append("-drive")
        qmounts.append(f"file={i},media=cdrom")

    assert image, f"image is empty {image}"
    if not os.path.exists(image):
        if os.path.exists(os.path.join(prefix, image)):
            image = os.path.join(prefix, image)
    assert os.path.exists(image), f"image not found {image}"
    threads = args.threads
    cmd = [
        "qemu-system-x86_64",
        "-enable-kvm",
        "-boot", "d",
        "-m", str(config.get("memory", 2048)),
        "-smp", str(threads),
        "-drive", f"file={image},if=virtio,index=0",
        "-cdrom", iso,
        "-net", "nic,model=virtio",
        "-vga", "virtio",
        "-cpu", "kvm64",
        "-chardev", "file,id=charserial0,path=gentoo.log",
        "-device", "isa-serial,chardev=charserial0,id=serial0",
        "-chardev", "pty,id=charserial1",
        "-device", "isa-serial,chardev=charserial1,id=serial1"
    ]
    # "-net", "user",
    # -net user: network backend 'user' is not compiled into this binary"

    cmd += qmounts
    LOG.info(cmd)
    proc = Popen(cmd, stderr=PIPE, stdout=PIPE)
    stdout, stderr = proc.communicate()
    if stderr:
        LOG.error(str(stderr))


