import subprocess
import sys
import os
from importlib.metadata import version, PackageNotFoundError
from qgis.core import (QgsVectorLayer,QgsExpressionContextUtils, QgsProject,Qgis,QgsApplication)  # pylint: disable=import-error
import re
from platform import python_version
import shutil


def install_cefpython3():
    python_V = python_version().split('.')
    python_V = 'Python'+str(python_V[0])+str(python_V[1])
    Qgispython_path =os.path.join(os.path.split(QgsApplication.prefixPath())[0],python_V) 

    qgis_python_path = os.path.join(Qgispython_path,'python') # Get the path to the QGIS Python interpreter
    site_packages = os.path.join(Qgispython_path,'Lib','site-packages')


    cefpython_install_command = [
        qgis_python_path, "-m", "pip", "install", "Cefpython3", "--target",site_packages 
    ]

  
    try:
        subprocess.check_call(cefpython_install_command)  # nosec B603 - fixed pip args, QGIS-bundled python
    #except subprocess Exception
    except subprocess.CalledProcessError as e:
        pass



def install_python_package(packagename):
    python_exe = os.path.join(sys.base_exec_prefix, "python.exe")
    command = [python_exe, "-m", "pip", "install", packagename, "-y"]
    try:
        result = subprocess.run(command, text=True, capture_output=True, check=True)  # nosec B603 - fixed pip args, no user input
        print("Cefpython3 was uninstalled successfully.")
        print(result.stdout)
    except subprocess.CalledProcessError as e:
        print(f"Failed to uninstall Cefpython3: {e}")
        print(e.stderr)


def uninstall_cefpython():
    python_exe = os.path.join(sys.base_exec_prefix, "python.exe")
    command = [python_exe, "-m", "pip", "uninstall", "cefpython3", "-y"]
    try:
        result = subprocess.run(command, text=True, capture_output=True, check=True)  # nosec B603 - fixed pip args, no user input
        print("Cefpython3 was uninstalled successfully.")
        print(result.stdout)
    except subprocess.CalledProcessError as e:
        print(f"Failed to uninstall Cefpython3: {e}")
        print(e.stderr)


def check_Cefpython_installation():
    cefpython_installed = False
    try:
        dist_version = version('cefpython3')

        if dist_version == '66.1':
            print("Cefpython3 version 66.1 is installed, which is not supported.")
            uninstall_cefpython()
        else:
            cefpython_installed = True
            print(f"Cefpython3 is installed with version {dist_version}, which is supported.")
    except PackageNotFoundError:
        print("Cefpython3 is not installed.")
    
    return cefpython_installed



def configure_paths():
    """Configures the environment to include the QGIS bin directory."""
    # Retrieve the QGIS version and split to get the main version part
    qgis_version = QgsExpressionContextUtils.globalScope().variable('qgis_version')
    qgis_version = qgis_version.split('-')[0]

    # Construct the source path dynamically
    qgis_bin_path = os.path.join(r"C:\Program Files\QGIS", qgis_version, "bin")

    # Append the constructed path to the system's PATH environment variable
    # It's often a safer choice to prepend (add in front) your path so it's searched first
    os.environ['PATH'] = qgis_bin_path + os.pathsep + os.environ['PATH']

    # Append the path to sys.path for Python to recognize it
    sys.path.append(qgis_bin_path)


def check_qgis_version():
    min_version = "3.22"
    max_version = "3.44"
    
    # Convert the version strings to tuples of integers for comparison
    min_version_tuple = tuple(map(int, min_version.split('.')))
    max_version_tuple = tuple(map(int, max_version.split('.')))
    # Truncate current version to same length as min/max (major.minor only)
    current_version_tuple = tuple(map(int, Qgis.QGIS_VERSION.split('-')[0].split('.')))[:2]

    if current_version_tuple < min_version_tuple or current_version_tuple > max_version_tuple:
        return False # Not supported
    return True # Supported






def copy_missen_DLLS(source, destination):
    try:
        shutil.copy(source, destination)
        print(f"Successfully copied {source} to {destination}")
    except IOError as e:
        print(f"Error occurred while copying {source}: {e}")
    except Exception as e:
        print(f"An error occurred: {e}")



def is_valid_file_path(file_path):
    return os.path.exists(file_path)


def get_Qgis_Version():
    # Get QGIS version from the global scope
    get_Qgis_version = QgsExpressionContextUtils.globalScope().variable('qgis_version')
    Qgis_major_minor_version = get_Qgis_version.split('-')[0]  # Major plus minor release
    print("Full QGIS version:", get_Qgis_version)
    
    split_path = Qgis_major_minor_version.rsplit(".", 1)
    Qgis_major_Version = split_path[0].strip("'")  # Major Release
    
    print("QGIS Major-Minor version:", Qgis_major_minor_version)
    print("QGIS Major version:", Qgis_major_Version)

    # Get the installation directory of QGIS
    Qgis_install_dir = QgsApplication.prefixPath()
    print("QGIS Installation Directory:", Qgis_install_dir)

    # Extract the generic installation directory up to "QGIS"
    base_install_dir = Qgis_install_dir
    while not base_install_dir.endswith("QGIS"):
        base_install_dir = os.path.dirname(base_install_dir)
        if os.path.basename(base_install_dir).startswith("QGIS"):
            break

    print("Base QGIS Installation Directory:", base_install_dir)

    return [Qgis_major_minor_version, Qgis_major_Version, base_install_dir]








def extract_python_Version():

    #python_version = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
    (major, minor) = sys.version_info.major, sys.version_info.minor
    # Extract the Python version using regular expressions
    #match = re.search(r'(\d+\.\d+\.\d+)', python_version)
    
    return major, minor



def is_valid_file_path(path):
    # Check if the directory exists
    return os.path.exists(path)

def check_DLLs():
    qgis_versions = get_Qgis_Version()
    Qgis_base_path = qgis_versions[2]  # Get the base path from the version information
    major, minor = extract_python_Version()  # Assuming this returns a string like "3.12"
    python_version = f"Python{major}{minor}"

    python_folder = os.path.join("apps", python_version, "DLLs")
    
    # Create paths to check for DLLs
    paths = [os.path.join(Qgis_base_path, python_folder)]

    # Conditional list of files based on the Python version
    files = []
    if (major, minor) < (3, 12):
        # Include specific DLLs for Python versions below 3.12
        files.extend(['libssl-1_1-x64.dll', 'libcrypto-1_1-x64.dll'])
    else:
        # Include specific DLLs for Python 3.12 and above
        files.extend(['libcrypto-3-x64.dll', 'libssl-3-x64.dll'])

    for path in paths:
        #print("Paths1",path)
        if is_valid_file_path(path):
            #print("Paths2",path)
            missing_files = [file for file in files if not os.path.exists(os.path.join(path, file))]
            print("missen files",missing_files)
            if not missing_files:
                print(f"All required DLLs are present in {path}.")
                return (False, path, None)  # No files are missing
            else:
                print(f"Missing {len(missing_files)} files in {path}: {', '.join(missing_files)}")
                return (True, path, missing_files)  # Return missing files as well

    print("No valid paths found for DLL checks.")
    return (True, path, files)


def return_Qgis_bin_path(path):
    # Split the path
    split_path = path.split("apps")[0]
    bin_path = os.path.join(split_path,"bin")
    return bin_path




        


    
    
    




    
    
    
    
    
    




    

    

   
 

    


