from typing import Any

from multiprocessing import cpu_count
from pathlib import Path

from scipy.io import wavfile

import torch
from fairseq import checkpoint_utils

from ultimate_rvc.typing_extra import F0Method

from ultimate_rvc.vc.infer_pack.models import (
    SynthesizerTrnMs256NSFsid,
    SynthesizerTrnMs256NSFsid_nono,
    SynthesizerTrnMs768NSFsid,
    SynthesizerTrnMs768NSFsid_nono,
)
from ultimate_rvc.vc.my_utils import load_audio
from ultimate_rvc.vc.vc_infer_pipeline import VC

SRC_DIR = Path(__file__).resolve().parent.parent


class Config:
    def __init__(self, device, is_half):
        self.device = device
        self.is_half = is_half
        self.n_cpu = 0
        self.gpu_name = None
        self.gpu_mem = None
        self.x_pad, self.x_query, self.x_center, self.x_max = self.device_config()

    def device_config(self) -> tuple:
        if torch.cuda.is_available():
            i_device = int(self.device.split(":")[-1])
            self.gpu_name = torch.cuda.get_device_name(i_device)
            if (
                ("16" in self.gpu_name and "V100" not in self.gpu_name.upper())
                or "P40" in self.gpu_name.upper()
                or "1060" in self.gpu_name
                or "1070" in self.gpu_name
                or "1080" in self.gpu_name
            ):
                print("16 series/10 series P40 forced single precision")
                self.is_half = False
                for config_file in ["32k.json", "40k.json", "48k.json"]:
                    with open(SRC_DIR / "vc" / "configs" / config_file) as f:
                        strr = f.read().replace("true", "false")
                    with open(SRC_DIR / "vc" / "configs" / config_file, "w") as f:
                        f.write(strr)
                with open(
                    SRC_DIR / "vc" / "trainset_preprocess_pipeline_print.py",
                ) as f:
                    strr = f.read().replace("3.7", "3.0")
                with open(
                    SRC_DIR / "vc" / "trainset_preprocess_pipeline_print.py",
                    "w",
                ) as f:
                    f.write(strr)
            else:
                self.gpu_name = None
            self.gpu_mem = int(
                torch.cuda.get_device_properties(i_device).total_memory
                / 1024
                / 1024
                / 1024
                + 0.4,
            )
            if self.gpu_mem <= 4:
                with open(
                    SRC_DIR / "vc" / "trainset_preprocess_pipeline_print.py",
                ) as f:
                    strr = f.read().replace("3.7", "3.0")
                with open(
                    SRC_DIR / "vc" / "trainset_preprocess_pipeline_print.py",
                    "w",
                ) as f:
                    f.write(strr)
        elif torch.backends.mps.is_available():
            print("No supported N-card found, use MPS for inference")
            self.device = "mps"
        else:
            print("No supported N-card found, use CPU for inference")
            self.device = "cpu"
            self.is_half = True

        if self.n_cpu == 0:
            self.n_cpu = cpu_count()

        if self.is_half:
            # 6G memory config
            x_pad = 3
            x_query = 10
            x_center = 60
            x_max = 65
        else:
            # 5G memory config
            x_pad = 1
            x_query = 6
            x_center = 38
            x_max = 41

        if self.gpu_mem != None and self.gpu_mem <= 4:
            x_pad = 1
            x_query = 5
            x_center = 30
            x_max = 32

        return x_pad, x_query, x_center, x_max


def load_hubert(device: str, model_path: str, *, is_half: bool) -> torch.nn.Module:
    models, _, _ = checkpoint_utils.load_model_ensemble_and_task(
        [model_path],
        suffix="",
    )
    hubert = models[0]
    hubert.to(device)

    hubert.half() if is_half else hubert.float()

    hubert.eval()
    return hubert


def get_vc(
    device: str,
    config: Config,
    model_path: str,
    *,
    is_half: bool,
) -> tuple[dict[str, Any], str, torch.nn.Module, int, VC]:
    cpt = torch.load(model_path, map_location="cpu")
    if "config" not in cpt or "weight" not in cpt:
        err_msg = (
            f"Incorrect format for {model_path}. Use a voice model trained using RVC v2"
        )
        raise ValueError(err_msg)

    tgt_sr = cpt["config"][-1]
    cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0]
    if_f0 = cpt.get("f0", 1)
    version = cpt.get("version", "v1")

    if version == "v1":
        if if_f0 == 1:
            net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=is_half)
        else:
            net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
    elif version == "v2":
        if if_f0 == 1:
            net_g = SynthesizerTrnMs768NSFsid(*cpt["config"], is_half=is_half)
        else:
            net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])
    else:
        err_msg = f"Unsupported model version: {version}"
        raise ValueError(err_msg)

    del net_g.enc_q
    net_g.load_state_dict(cpt["weight"], strict=False)
    net_g.eval().to(device)

    net_g.half() if is_half else net_g.float()

    vc = VC(tgt_sr, config)
    return cpt, version, net_g, tgt_sr, vc


def rvc_infer(
    index_path: str,
    index_rate: float,
    input_path: str,
    output_path: str,
    pitch_change: int,
    f0_method: F0Method,
    cpt: dict[str, Any],
    version: str,
    net_g: torch.nn.Module,
    filter_radius: int,
    tgt_sr: int,
    rms_mix_rate: float,
    protect: float,
    crepe_hop_length: int,
    vc: VC,
    hubert_model: torch.nn.Module,
    resample_sr: int,
) -> None:
    audio = load_audio(input_path, 16000)
    times = [0, 0, 0]
    if_f0 = cpt.get("f0", 1)
    audio_opt, output_sr = vc.pipeline(
        hubert_model,
        net_g,
        0,
        audio,
        input_path,
        times,
        pitch_change,
        f0_method,
        index_path,
        index_rate,
        if_f0,
        filter_radius,
        tgt_sr,
        resample_sr,
        rms_mix_rate,
        version,
        protect,
        crepe_hop_length,
    )
    wavfile.write(output_path, output_sr, audio_opt)
