#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#   Copyright 2016-2026 Blaise Frederick
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
#
import argparse
from argparse import Namespace
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

import numpy as np
from numpy.typing import NDArray

import rapidtide.io as tide_io
import rapidtide.util as tide_util


def _get_parser() -> Any:
    """
    Create and configure argument parser for tcfrom2col command line tool.

    This function sets up an argument parser with required and optional arguments
    for processing two-column data files into time course output.

    Returns
    -------
    argparse.ArgumentParser
        Configured argument parser object with all required and optional arguments
        for the tcfrom2col tool.

    Notes
    -----
    The parser includes the following required arguments:
    - infilename: input two column file name
    - timestep: time step of output time course in seconds
    - numpoints: number of output time points
    - outfilename: output time course file name

    Examples
    --------
    >>> parser = _get_parser()
    >>> args = parser.parse_args()
    """
    # get the command line parameters
    parser = argparse.ArgumentParser(
        prog="tcfrom2col",
        description="Plots the data in text files.",
        allow_abbrev=False,
    )
    parser.add_argument("infilename", help="the name of the input two column file")
    parser.add_argument(
        "timestep",
        type=float,
        help="the time step of the output time course in seconds",
    )
    parser.add_argument("numpoints", type=int, help="the number of output time points")
    parser.add_argument("outfilename", help="the name of the output time course file")
    parser.add_argument(
        "--debug",
        action="store_true",
        help="turn on additional debugging output",
        default=False,
    )
    return parser


def tcfrom2col(args: Any) -> None:
    """
    Convert two-column data to tidal constituent data.

    This function reads two-column input data, processes it to extract tidal constituents,
    and writes the results to an output file. The input data is expected to be in a format
    suitable for tidal analysis, and the output contains the computed tidal constituents.

    Parameters
    ----------
    args : Any
        An object containing the following attributes:
        - infilename : str
            Path to the input file containing two-column data
        - outfilename : str
            Path to the output file for writing tidal constituent data
        - numpoints : int
            Number of data points in the time series
        - timestep : float
            Time step between data points
        - debug : bool
            Flag to enable debug printing

    Returns
    -------
    None
        This function does not return a value but writes output to a file.

    Notes
    -----
    The function uses `tide_io.readvecs` to read input data and `tide_util.maketcfrom2col`
    to perform the tidal constituent calculation. The time axis is generated based on the
    number of points and time step provided in the args object.

    Examples
    --------
    >>> class Args:
    ...     def __init__(self):
    ...         self.infilename = 'input.txt'
    ...         self.outfilename = 'output.txt'
    ...         self.numpoints = 1000
    ...         self.timestep = 300.0
    ...         self.debug = False
    ...
    >>> args = Args()
    >>> tcfrom2col(args)
    """
    if args.debug:
        print(args)

    # now make the vector
    inputdata = tide_io.readvecs(args.infilename)
    timeaxis = np.arange(0.0, args.numpoints * args.timestep, args.timestep)
    outputdata = 0.0 * timeaxis
    outputdata = tide_util.maketcfrom2col(inputdata, timeaxis, outputdata, debug=args.debug)
    tide_io.writenpvecs(outputdata, args.outfilename)
