#!/usr/bin/env python

import os
from mpi4py import MPI
from netCDF4 import Dataset

from .nc_projections import *


def open_netcdf(path, comm=None, xarray=False, info=False, parallel_method='Y', 
                avoid_first_hours=0, avoid_last_hours=0,
                first_level=0, last_level=None, balanced=False):
    """
    Open a netCDF file

    Parameters
    ----------
    path : str
        Path to the NetCDF file to read
    comm : MPI.COMM
        MPI communicator to use in that netCDF. Default: MPI.COMM_WORLD
    xarray : bool
        (Not working) Indicates if you want to use xarray. Default: False
    info : bool
        Indicates if you want to print (stdout) the reading/writing steps
    avoid_first_hours : int
        Number of hours to remove from first time steps.
    avoid_last_hours : int
        Number of hours to remove from last time steps.
    parallel_method : str
        Indicates the parallelization method that you want. Default: 'Y' (over Y axis)
        accepted values: ['X', 'Y', 'T']

    Returns
    -------
    Nes
        Nes object. Variables read in lazy mode (only metadata)
    """
    if comm is None:
        comm = MPI.COMM_WORLD
    else:
        comm = comm

    if not os.path.exists(path):
        raise FileNotFoundError(path)
    if xarray:
        dataset = None
    else:
        # dataset = Dataset(path, format="NETCDF4", mode='r', parallel=False)
        if comm.Get_size() == 1:
            dataset = Dataset(path, format="NETCDF4", mode='r', parallel=False)
        else:
            dataset = Dataset(path, format="NETCDF4", mode='r', parallel=True, comm=comm, info=MPI.Info())
    if __is_rotated(dataset):
        # Rotated grids
        nessy = RotatedNes(comm=comm, dataset=dataset, xarray=xarray, info=info, parallel_method=parallel_method,
                           avoid_first_hours=avoid_first_hours, avoid_last_hours=avoid_last_hours,
                           first_level=first_level, last_level=last_level, create_nes=False, balanced=balanced,)
    elif __is_points(dataset):
        if __is_points_GHOST(dataset):
            # Points - GHOST
            nessy = PointsNesGHOST(comm=comm, dataset=dataset, xarray=xarray, info=info,
                                   parallel_method=parallel_method,
                                   avoid_first_hours=avoid_first_hours, avoid_last_hours=avoid_last_hours,
                                   first_level=first_level, last_level=last_level, create_nes=False, balanced=balanced,)
        else:
            # Points - non-GHOST
            nessy = PointsNes(comm=comm, dataset=dataset, xarray=xarray, info=info, parallel_method=parallel_method,
                              avoid_first_hours=avoid_first_hours, avoid_last_hours=avoid_last_hours,
                              first_level=first_level, last_level=last_level, create_nes=False, balanced=balanced,)
    elif __is_lcc(dataset):
        # Lambert conformal conic grids
        nessy = LCCNes(comm=comm, dataset=dataset, xarray=xarray, info=info, parallel_method=parallel_method,
                       avoid_first_hours=avoid_first_hours, avoid_last_hours=avoid_last_hours,
                       first_level=first_level, last_level=last_level, create_nes=False, balanced=balanced,)
    elif __is_mercator(dataset):
        # Mercator grids
        nessy = MercatorNes(comm=comm, dataset=dataset, xarray=xarray, info=info, parallel_method=parallel_method,
                            avoid_first_hours=avoid_first_hours, avoid_last_hours=avoid_last_hours,
                            first_level=first_level, last_level=last_level, create_nes=False, balanced=balanced,)
    else:
        # Regular grids
        print('1.2 ' * 20)
        nessy = LatLonNes(comm=comm, dataset=dataset, xarray=xarray, info=info, parallel_method=parallel_method,
                          avoid_first_hours=avoid_first_hours, avoid_last_hours=avoid_last_hours,
                          first_level=first_level, last_level=last_level, create_nes=False, balanced=balanced,)

    return nessy


def __is_rotated(dataset):
    """
    Check if the netCDF is in rotated pole projection or not.

    Parameters
    ----------
    dataset : Dataset
        netcdf4-python opened dataset object

    Returns
    -------
    value : bool
        Indicated if the netCDF is a rotated one
    """
    if 'rotated_pole' in dataset.variables.keys():
        return True
    else:
        return False


def __is_points(dataset):
    """
    Check if the netCDF is a points dataset in non-GHOST format or not.

    Parameters
    ----------
    dataset : Dataset
        netcdf4-python opened dataset object

    Returns
    -------
    value : bool
        Indicated if the netCDF is a points non-GHOST one
    """
    if 'station' in dataset.dimensions:
        return True
    else:
        return False


def __is_points_GHOST(dataset):
    """
    Check if the netCDF is a points dataset in GHOST format or not.

    Parameters
    ----------
    dataset : Dataset
        netcdf4-python opened dataset object

    Returns
    -------
    value : bool
        Indicated if the netCDF is a points GHOST one
    """
    if 'N_flag_codes' in dataset.dimensions and 'N_qa_codes' in dataset.dimensions:
        return True
    else:
        return False

def __is_lcc(dataset):
    """
    Check if the netCDF is in Lambert Conformal Conic (LCC) projection or not.

    Parameters
    ----------
    dataset : Dataset
        netcdf4-python opened dataset object

    Returns
    -------
    value : bool
        Indicated if the netCDF is a LCC one
    """
    if 'Lambert_conformal' in dataset.variables.keys():
        return True
    else:
        return False 

def __is_mercator(dataset):
    """
    Check if the netCDF is in Mercator projection or not.

    Parameters
    ----------
    dataset : Dataset
        netcdf4-python opened dataset object

    Returns
    -------
    value : bool
        Indicated if the netCDF is a Mercator one
    """
    if 'mercator' in dataset.variables.keys():
        return True
    else:
        return False 

def concatenate_netcdfs(nessy_list, comm=None):
    if not isinstance(nessy_list, list) or len(nessy_list) < 2:
        raise AttributeError("You must pass a list of at least two elements (NES or paths).")

    if isinstance(nessy_list[0], str):
        nessy_first = open_netcdf(nessy_list[0], comm=comm)
    else:
        nessy_first = nessy_list[0]
    for i, aux_nessy in enumerate(nessy_list[1:]):
        if isinstance(aux_nessy, str):
            aux_nessy = open_netcdf(aux_nessy, comm=comm)
        nessy_first.concatenate(aux_nessy)
    return nessy_first
