PlotTimeSeries <- function(var, colour_along = NULL, style_along = NULL,
                           extremes_along = NULL, boxwhisker_along = NULL, 
                           group_along = NULL, conf_int_along = NULL, 
                           dates = NULL, sdates = NULL, period = 1, freq = 12, 
                           dateini = NULL, monini = NULL, yearini = NULL, 
                           group_along_memb = TRUE, group_along_ensmean = TRUE,
                           show_sig_lev = TRUE, show_conf_int = TRUE,
                           title = NULL, xtitle = NULL, ytitle = NULL,
                           legend = TRUE, multiInOne = FALSE,
                           fileout = NULL, pathToFile = NULL,
                           fileWidth = NA, fileHeight = NA, 
                           unitsFileSize = "in") {
  # Dependencies: to be deleted from the function
  library(ggplot2)
  library(reshape)

  # Check 'along' arguments
  #------------------------
  if (!is.null(colour_along) && !is.numeric(colour_along) && !is.character(colour_along)) {
    stop("'colour_along' must be a character string or a number")
  }
  if (is.numeric(colour_along)) {
    colour_along <- round(colour_along) 
  }

  if (!is.null(style_along) && !is.numeric(style_along) && !is.character(style_along)) {
    stop("'style_along' must be a character string or a number")
  }
  
  if (!is.null(extremes_along) && !is.numeric(extremes_along) && !is.character(extremes_along)) {
    stop("'extremes_along' must be a character string or a number")
  }
  
  if (!is.null(boxwhisker_along) && !is.numeric(boxwhisker_along) && !is.character(boxwhisker_along)) {
    stop("'boxwhisker_along' must be a character string or a number")
  }
  
  if (!is.null(group_along) && !is.numeric(group_along) && !is.character(group_along)) {
    stop("'group_along' must be a character string or a number")
  }
  
  if (!is.null(conf_int_along) && !is.numeric(conf_int_along) && !is.character(conf_int_along)) {
    stop("'conf_int_along' must be a character string or a number")
  }
  
  if (is.null(colour_along) && is.null(style_along) && is.null(extremes_along) && is.null(boxwhisker_along) && is.null(group_along) && is.null(conf_int_along)) {
    ## TODO: Process var to guess best approach
  }

  ## TODO: Take into account the parameters 'group_along_memb', 
  ## 'group_along_ensmean', 'show_sig_lev', 'show_conf_int'

  #-------------------------------------
  # Check parameters and process them
  #-------------------------------------
  # Check var
  #----------
  ## TODO: Check names(dim(data))
  ##       

  ## TODO: Change to general, now var is assumed to be an array of 
  ## dimensions  c("dataset", "member", "sdate", "ftime") and we
  ## supose that 'colour_along' is one of those names --> TODO CHECK
  if (!(colour_along %in% c("member", "ftime", "dataset")) && !is.null(colour_along)) {
    .warning("Unidentified 'colour_along' value, guessing from data")
    ## TODO: Guessing value from data
    ## From here, colour_along has the correct value (null or identified dim)
  } 


  ## TODO: Assumed that dates is in POSIXct, list of sdates 
  ## (TODO :change and adapt to correct format )

  ## Assign array to variable data
  data <- var

  ## TODO: Plot obs and exp at the same time in the plot. Now only one 

  # Check plot parameters
  #---------------------------
  # Calculate period value from period / freq / var
  if (is.null(period) && is.null(freq)) {
    ## TODO: process var and overwrite period value
  } else if (is.null(period)) {
    if (is.character(freq)) {
      period <- .FreqNameToPeriodInt(freq)
      if (period == 0) {
        .warning("'freq' name not found, guessing from data")
        ## TODO: process var and overwrite period value
      }
    } else if (!is.character(freq) && !is.numeric(freq)) {
      .warning("Wrong 'freq' value, guessing from data")
      ## TODO: process var and overwrite period value
    } else {
      # Extract the period value
      period <- 12 / freq
    }
  } else {
    if (is.character(period)) {
      period <- .PeriodNameToInt(period)
      if (period == 0) {
        .warning("'period' name not found, guessing from data")
        ## TODO: process var and overwrite period value
      }
    } else if (!is.character(period) && !is.numeric(period)) {
      .warning("Wrong 'period' value, guessing from data")
      ## TODO: process var and overwrite period value
    }
  }

  # Check correct period integer value
  ## TODO

  # Calculate dates value from dates / sdates / var
  ## TODO

  # Check correct dates value
  ## TODO

  # Calculate dates dateini from dateini / monini / yearini / var
  ## TODO

  # Check correct dateini value
  ## TODO

  # Check graphical parameters
  #---------------------------
  if (is.null(title)) {
    ## TODO: process var
  } else if (!is.character(title)) {
    stop("'title' must be a character string")
    ## TODO: Should it be mandatory? labs accept more types
  } 

  if (is.null(xtitle)) {
    ## TODO: process var
  } else if (!is.character(xtitle)) {
    stop("'xtitle' must be a character string")
    ## TODO: Should it be mandatory? labs accept more types
  } 
  
  if (is.null(ytitle)) {
    ## TODO: process var
  } else if (!is.character(ytitle)) {
    stop("'ytitle' must be a character string")
    ## TODO: Should it be mandatory? labs accept more types
  } 

  if (is.logical(legend)) {
    ## TODO
  } else {
    ## TODO
  }

  ## TODO: if var is a list consider how to divide facets by default to 
  ## plot variables in multipanel, as with one var it can be plotted its 
  ## content in multipanel too

  # Check device
  #-----------------------------
  if (!is.character(fileout) && !is.null(fileout)) {
    stop("Parameter 'fileout' must be a character string.")
  }
  if (is.null(fileout) && !is.null(pathToFile)) {
    .warning("'fileout' not specified for the 'pathToFile', it will be 
              ignored")
  } else if (!is.null(fileout)) {
    # Chek pathToFile
    if (is.null(pathToFile)) {
      pathToFile <- getwd()
    } else if (!is.character(pathToFile)) {
      .warning("'pathToFile' must be a character string of the desired path.
                Saving to default (working directory)")
      pathToFile <- getwd()
    } ## TODO: check if it is a correct pathname?
    
    # Check size parameters
    if (!is.numeric(fileWidth) && !is.na(fileWidth)) {
      .warning("'fileWidth' must be a number, setting width to default")
      fileWidth <- NA
    }
    if (!is.numeric(fileHeight) && !is.na(fileHeight)) {
      .warning("'fileHeight' must be a number, setting height to default")
      fileHeight <- NA
    }
    if (!(unitsFileSize %in% c("cm", "in", "mm"))) {
      .warning("'unitsFileSize' must be 'cm', 'in' or 'mm', setting units to 
                default (inches)")
      unitsFileSize <- "in"
    }
  }

  #

  #-------------------------------------
  # Plot
  #-------------------------------------

  # Melting data to DF
  mdata <- melt.array(data, varnames = names(dim(data)))

  ## TODO: Fill the ggplot parameters!!
  ##       Adapt to general
  g <- ggplot(mdata, aes(x = sdate , y = value))

  # Add lines
  ## TODO: make it general, only now for colour_along on member!!
  g <- g + geom_line(aes(color = as.factor(colour_along)))

  # Add title and axes labels
  ## TODO: will be uncommented
  ## g <- g + labs(title = title, x = xtitle, y = ytitle)
  if (is.logical(legend) && !legend) {
    # Disable legends
    g <- g + theme(legend.position="none")
  } else {
    # Remove the title of the legend
    # g <- g + guides(fill=guide_legend(title=NULL))
    # Change the names of the labels
    ## TODO: Names labels and axis
  }

  #-------------------------------------
  # Return ggplot2 object or save to file
  #-------------------------------------

  if (!is.null(fileout)) {
    # Save to file
    g <- ggsave(filename = fileout, plot = g, path = pathToFile,
           width = fileWidth, height = fileHeight, units = unitsFileSize)
  } 

  # Return NULL or the ggplot2 object
  g
}

