#!/usr/bin/env python3

import click
import cloup
from cloup.constraints import mutually_exclusive, RequireExactly, IsSet, If
import numpy as np
import warnings
warnings.filterwarnings("ignore")
from MapLines.tools.line_fit import line_fit
import MapLines.tools.tools as tol
import os.path as ptt

@click.group('run_mapline', short_help='CLI for the emission line mapper')
def cli():
    pass


@cloup.command(short_help='Run the emission line mapper')
@click.option('-n', '--name', type=str, help='name of the IFS cube')
@click.option('-o', '--name_out', type=str, default='None', help='name of the output files')
@click.option('-m', '--mask', type=str, default='test', help='name of the mask map')
@click.option('-p', '--path',     type=str, default='', help='path to the data cubes')
@click.option('-y', '--path_out', type=str, default='outputs/', help='path of the output files')
@click.option('-c', '--ncpus', type=int, default=10, help='number of CPUs')
@click.option('-d', '--double', is_flag=True, default=False,help='flag to run double peak mode')
@click.option('-k', '--kskew', is_flag=True, default=False,help='flag to run skew line profile mode')
@click.option('-t', '--test', is_flag=True, default=False,help='flag to run the full analysis')
@click.option('-e', '--error', is_flag=True, default=False,help='flag to run autocalculate the error vector')
@click.option('-z', '--zt', type=float, default=0, help='redshift of the object')
@click.option('-r', '--rvel', type=float, default=500.0, help='velocity shift range')
@click.option('-b', '--bcont', is_flag=True, default=False,help='flag to run deactivate autosubstract continum')
@click.option('-s', '--sprogressd', is_flag=True, default=False,help='deactivate the progress bar')
@click.option('-i', '--it', type=int, default=0, help='i index for test, default nx/2')
@click.option('-j', '--jt', type=int, default=0, help='j index for test, default ny/2')
@click.option('-h', '--hbet', is_flag=True, default=False,help='fit the hb region')

def run(name,mask,path,path_out,zt,rvel,ncpus,test,error,double,bcont,it,jt,sprogressd,hbet,name_out,kskew):
    

    if name_out == 'None':
        name_out=name

    if test:
        pgr_bar=False
        plot_f=True
    else:
        plot_f=False
        if sprogressd:
            pgr_bar=False
        else:
            pgr_bar=True
    if bcont:
        cont=False
    else:
        cont=True
    if double:
        lab='_double'
        single=False
    else:
        lab='_single'
        single=True
    if error:
        error_c=False
    else:
        error_c=True
    if hbet:
        lA1=4700.0
        lA2=5050.0
        lbh='_hb'
        hbfit=True
    else:
        lA1=6350.0
        lA2=6850.0
        lbh='_ha' 
        hbfit=False
    if kskew:
        skew=True
        skl='_skew'
    else:
        skew=False
        skl=''    

    dirt=path
    dir_out=path_out
    file1=dirt+'NAME.fits.gz'.replace('NAME',name)
    file2=dirt+'NAME.fits.gz'.replace('NAME',name)
    file3=dirt+'NAMEM.fits.gz'.replace('NAMEM',mask)  
    if ptt.exists(dir_out) == False:
        tol.sycall('mkdir -p '+dir_out)
    file_out=dir_out+'NAME_modelsV2'.replace('NAME',name_out)+lab+lbh+skl
    file_out2=dir_out+'NAME_paramV2'.replace('NAME',name_out)+lab+lbh+skl
    name_out2=name_out+lab+lbh+skl
    line_fit(file1,file2,file3,file_out,file_out2,name_out2,z=zt,lA1=lA1,lA2=lA2,plot_f=plot_f,pgr_bar=pgr_bar,ncpu=ncpus,
         single=single,skew=skew,flux_f=1.0,erft=0.75,dv1t=rvel,sim=True,cont=cont,hbfit=hbfit,test=test,error_c=error_c,i_t=it,j_t=jt)
    
cli.add_command(run)


if __name__ == "__main__":
    cli()
