#!/usr/bin/env python

##
##  Copyright 2017 SRI International
##  See COPYING file distributed along with the package for the copyright and license terms
##

from __future__ import print_function
from builtins import next
import os
import sys
import filecmp
import glob
import re
from tabnanny import verbose
import pandas as pd 
from unittest.mock import patch
import pytest
import sibispy
from sibispy.tests.utils import get_session
current_dir=os.path.dirname(__file__)
# sys.path.append(os.path.join(current_dir, '../../../'))
sys.path.append(os.path.join(current_dir, '../../../scripts/xnat'))
# import miqa_file_generation_ as miqa_file_generation
import miqa_file_generation
import upload_visual_qc


@pytest.fixture
def project_list():
    return ["DUKE", "OHSU", "SRI", "UCSD", "UPMC"]


@pytest.fixture
def sibis_session():
    sibispy.sibislogger.init_log(verbose=True)

    sibis_session = sibispy.Session()
    if not sibis_session.configure():
        print("Error: session configure file was not found")
        sys.exit()
    
    return sibis_session

@pytest.mark.parametrize("file_prefix",
                         ["test_miqa_file_generation",
                          "test_miqa_file_generation_2"])
def test_read_write_json(file_prefix, sibis_session, project_list):
    # Read Json file
    file_name=file_prefix +".json"
    orig_file=os.path.join(current_dir,file_name)
    assert(os.path.exists(str(orig_file)))    
    json_dict = miqa_file_generation.read_miqa_import_file(file_name, current_dir)

    # Write out Json File
    created_file=os.path.join("/tmp",file_name)
    if os.path.exists(created_file):
        os.remove(created_file)
        
    miqa_file_generation.write_miqa_import_file(json_dict, file_name, "/tmp")
     
    # Compare_File
    # print(orig_file,created_file)
    assert(filecmp.cmp(orig_file,created_file))


@pytest.mark.parametrize("file_prefix",
                         ["test_miqa_file_generation_bad"])
def test_read_bad_json(file_prefix, sibis_session, project_list):
    # Read Json file
    file_name=file_prefix +".json"
    orig_file=os.path.join(current_dir,file_name)
    assert(os.path.exists(str(orig_file)))    
    json_dict = miqa_file_generation.read_miqa_import_file(file_name, current_dir)
    assert json_dict == {}, "Json dict should be empty bc read from bad file"
 
@pytest.mark.parametrize("file_prefix",
                        [
                            "test_miqa_file_generation",
                            "test_miqa_file_generation_2"
                        ])
def test_json_convert_check_new_sessions_df(file_prefix):
    # Read Json file
    # print(" test_json_convert_check_new_sessions_df", file_prefix)
    file_name=file_prefix +".json"
    orig_file=os.path.join(current_dir,file_name)
    assert(os.path.exists(str(orig_file)))    
    json_dict = miqa_file_generation.read_miqa_import_file(file_name, current_dir)
    json_df: pd.DataFrame = miqa_file_generation.convert_json_to_check_new_sessions_df(json_dict)

    # Read legacy CSV file
    csv_file=os.path.join(current_dir,file_prefix + ".csv")
    csv_df: pd.DataFrame = upload_visual_qc.read_csf_file(csv_file)
    
    # sort so we are comparing similar thing
    dataframe_cols = csv_df.columns.to_list()
    json_df = json_df.sort_values(by=dataframe_cols).reset_index(drop=True)
    csv_df = csv_df.sort_values(by=dataframe_cols).reset_index(drop=True)
    
    df_diffs = csv_df.compare(json_df)
    assert len(df_diffs) == 0, "DataFrames should be identical."

    


@pytest.mark.parametrize("file_name",
                        [
                            "test_miqa_file_generation_write.json"
                        ])
def test_write_to_json_(file_name):

    scans_to_qc = ["xnat_experiment_id,nifti_folder,scan_id,scan_type,experiment_note,decision,scan_note\n"]
    scans_to_qc.append('NCANDA_E11836,/fs/storage/XNAT/archive/sri_incoming/arc001/B-00350-M-2-20221014/RESOURCES/nifti,3,ncanda-t2fse-v1,"",,""\n')

    created_file=os.path.join("/tmp",file_name)
    if os.path.exists(created_file):
        os.remove(created_file)

    successFlag = miqa_file_generation.write_miqa_import_file(scans_to_qc, file_name, "/tmp")

    assert successFlag == False, "Writing Json file should have failed bc it is not correct dictionary" 

    # turn into json dictionary
    rows_cols = [row.split(',') for row in scans_to_qc]
    df = pd.DataFrame(rows_cols[1:], columns=rows_cols[0])
    new_df = miqa_file_generation.convert_dataframe_to_new_format(df)
    scans_to_qc_json_dict = miqa_file_generation.import_dataframe_to_dict(new_df)

    successFlag = miqa_file_generation.write_miqa_import_file(scans_to_qc_json_dict, file_name, "/tmp")

    assert successFlag == True, "Writing Json file should have been successsfull " 

@@ -322,17 +322,16 @@
 as as pd
- 
 %0Afrom un
@@ -1031,20 +1031,16 @@
 .exit()%0A
-    
 %0A    ret
@@ -1419,36 +1419,32 @@
 str(orig_file)))
-    
 %0A    json_dict =
@@ -1655,24 +1655,16 @@
 d_file)%0A
-        
 %0A    miq
@@ -1734,21 +1734,16 @@
 %22/tmp%22)%0A
-     
 %0A    # C
@@ -2146,36 +2146,32 @@
 str(orig_file)))
-    
 %0A    json_dict =
@@ -2316,17 +2316,16 @@
 d file%22%0A
- 
 %0A@pytest
@@ -2800,20 +2800,16 @@
 _file)))
-    
 %0A    jso
@@ -3138,20 +3138,16 @@
 v_file)%0A
-    
 %0A    # s
@@ -3380,20 +3380,16 @@
 p=True)%0A
-    
 %0A    df_
@@ -3490,15 +3490,11 @@
 .%22%0A%0A
-    %0A
 %0A%0A
+%0A
 @pyt
@@ -4259,17 +4259,16 @@
 tionary%22
- 
 %0A%0A    # 
@@ -4314,16 +4314,34 @@
  = %5Brow.
+replace('%5Cn', '').
 split(',
@@ -4766,10 +4766,9 @@
 ssfull %22
- 
 %0A

Remove newline characters from input dataframe