#!/usr/bin/env python
# -*- coding: latin-1 -*-
#
#   Copyright 2016-2021 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

import numpy as np

import rapidtide.correlate as tide_corr
import rapidtide.fit as tide_fit
import rapidtide.io as tide_io
import rapidtide.miscmath as tide_math
import rapidtide.resample as tide_resample
import rapidtide.workflows.aligntcs as aligntcs_main
import rapidtide.workflows.parser_funcs as pf


def _get_parser():
    # get the command line parameters
    parser = argparse.ArgumentParser(
        prog="aligntcs",
        description="Resample and align two time series.",
        usage="%(prog)s infile1 insamplerate1 infile2 insamplerate2 outputfile [options]",
    )

    # Required arguments
    parser.add_argument(
        "infile1",
        metavar="infile1[:COLNUM]",
        type=str,
        help="text file containing a timeseries.  Select column COLNUM if multicolumn file.",
    )
    parser.add_argument(
        "insamplerate1",
        type=lambda x: pf.is_float(parser, x),
        help="The input data file (BOLD fmri file or NIRS text file)",
    )
    parser.add_argument(
        "infile2",
        metavar="infile2[:COLNUM]",
        type=str,
        help="text file containing a timeseries.  Select column COLNUM if multicolumn file.",
    )
    parser.add_argument(
        "insamplerate2",
        type=lambda x: pf.is_float(parser, x),
        help="The input data file (BOLD fmri file or NIRS text file)",
    )
    parser.add_argument("outputfile", help="The name of the output file")

    parser.add_argument(
        "--nodisplay",
        dest="display",
        action="store_false",
        help=("Do not plot the data (for noninteractive use)"),
        default=True,
    )
    parser.add_argument(
        "--verbose",
        dest="verbose",
        action="store_true",
        help=("Print out more debugging information"),
        default=False,
    )
    pf.addsearchrangeopts(parser)

    # Filter arguments
    pf.addfilteropts(parser, "timecourses")

    return parser


def main(inputargs=None):
    args, argstowrite = pf.setargs(_get_parser, inputargs=inputargs)

    if args.display:
        import matplotlib as mpl

        mpl.use("TkAgg")
        import matplotlib.pyplot as plt

    args = pf.postprocesssearchrangeopts(args)
    args, theprefilter = pf.postprocessfilteropts(args)

    intimestep1 = 1.0 / args.insamplerate1
    intimestep2 = 1.0 / args.insamplerate2

    inputdata1 = tide_io.readcolfromtextfile(args.infile1)
    inputdata2 = tide_io.readcolfromtextfile(args.infile2)

    # determine waveform lengths
    time1 = args.insamplerate1 * (len(inputdata1) - 1)
    time2 = args.insamplerate2 * (len(inputdata2) - 1)

    fulltime = np.max([time1, time2])
    # pad waveform1 if it's shorter than waveform2
    if time1 < fulltime:
        paddeddata1 = np.zeros(int(np.ceil(fulltime // intimestep1)), dtype=float)
        paddeddata1[0 : len(inputdata1) + 1] = tide_math.corrnormalize(
            theprefilter.apply(args.insamplerate1, inputdata1)
        )
    else:
        paddeddata1 = tide_math.corrnormalize(theprefilter.apply(args.insamplerate1, inputdata1))

    timeaxisfull = np.linspace(
        0.0, intimestep1 * len(paddeddata1), num=len(paddeddata1), endpoint=False
    )
    timeaxis1 = np.linspace(
        0.0, intimestep1 * len(inputdata1), num=len(inputdata1), endpoint=False
    )
    timeaxis2 = np.linspace(
        0.0, intimestep2 * len(inputdata2), num=len(inputdata2), endpoint=False
    )
    paddeddata2 = tide_resample.doresample(
        timeaxis2,
        tide_math.corrnormalize(theprefilter.apply(args.insamplerate2, inputdata2)),
        timeaxisfull,
    )

    # now paddeddata1 and 2 are on the same timescales
    thexcorr = tide_corr.fastcorrelate(paddeddata1, paddeddata2)
    xcorrlen = len(thexcorr)
    xcorr_x = (
        np.r_[0.0:xcorrlen] * intimestep1 - (xcorrlen * intimestep1) / 2.0 + intimestep1 / 2.0
    )

    (
        maxindex,
        maxdelay,
        maxval,
        maxsigma,
        maskval,
        failreason,
        peakstart,
        peakend,
    ) = tide_fit.findmaxlag_gauss(
        xcorr_x,
        thexcorr,
        args.lagmin,
        args.lagmax,
        1000.0,
        refine=True,
        useguess=False,
        fastgauss=False,
        displayplots=False,
    )

    print("Crosscorrelation_Rmax:\t", maxval)
    print("Crosscorrelation_maxdelay:\t", maxdelay)

    # now align the second timecourse to the first

    aligneddata2 = tide_resample.doresample(timeaxis2, inputdata2, timeaxis1 - maxdelay)
    tide_io.writevec(aligneddata2, args.outputfile)

    if args.display:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        # ax.set_title('GCC')
        plt.plot(xcorr_x, thexcorr, "k")
        plt.show()
        fig = plt.figure()
        plt.plot(timeaxis1, inputdata1)
        plt.plot(timeaxis1, aligneddata2)
        plt.plot()
        plt.show()


if __name__ == "__main__":
    """try:
        args = _get_parser().parse_args()
    except SystemExit:
        _get_parser().print_help()
        raise"""

    aligntcs_main(inputargs=None)
