import os
import glob
from numpy.core.numeric import NaN
import numpy as np
import datetime
import pandas as pd


class DataPreprocessor:
    def __init__(self):
        self.image_format = '.png'
        self.file_extension = '.csv'

    def pre_process(self):
        cwd = os.getcwd()
        os.chdir(cwd + '/dataeng/02-src-data')
        all_filenames = [i for i in glob.glob(f"*{self.file_extension}")]

        dfs = []
        for file in all_filenames:
            df = pd.read_csv(file)
            df['user_id'] = file[:len(file) - 4]
            if glob.glob(os.getcwd() + '/' + file[:len(file) - 4] + self.image_format): 
                df['img_path'] = (os.getcwd() + '/' + file[:len(file) - 4]) + self.image_format
            dfs.append(df)

        combined_csv_data = pd.concat(dfs)
        current_df = pd.read_csv('../processed_data/output.csv')
        if(current_df.ne(combined_csv_data).all(axis=1).any()):
            print('processed')
            combined_csv_data.to_csv('../processed_data/output.csv', index=False)
            return combined_csv_data
        return current_df

    def process_by_params(self, *args):
        df = self.pre_process()
        if all(v is None for v in args):
            return df
        
        is_image_exists, min_age, max_age = args
        if is_image_exists is not None:
            df = df.loc[(~df['img_path'].isin([NaN]))] if is_image_exists else  df.loc[(df['img_path'].isin([NaN]))] 
        
        if min_age is not None or max_age  is not None:
            now = pd.Timestamp('now')
            df['norm_date_birth'] = pd.to_datetime(df[' birthts'], unit='ms')
            df['norm_date_birth'] = df['norm_date_birth'].where(df['norm_date_birth'] < now, df['norm_date_birth'] -  np.timedelta64(100, 'Y'))
            df['age'] = (now - df['norm_date_birth']).astype('<m8[Y]') 
            if min_age is not None:
                df = df.loc[df['age']>=int(min_age)] 
            if max_age is not None:
                df = df.loc[df['age']<=int(max_age)] 
            df = df.drop(['norm_date_birth', 'age'], axis=1)

        return df

    def save_solo_data(self, new_df):
        df = self.pre_process()
        if new_df is None:
            return df
        df.append(new_df)
        new_df.to_csv('/dataeng/02-src-data/'+new_df['user_id'] + '.csv', index=False)
        df = self.pre_process()
        return df
        



