import os
import sys
import glob
import numpy as np

import keras
import tensorflow as tf
from keras import Model
from keras import backend as K
from keras.regularizers import l2
from keras.engine import Layer,InputSpec
from keras.layers.merge import concatenate
from keras.callbacks import TensorBoard,Callback
from keras.layers.advanced_activations import LeakyReLU
from keras.preprocessing.image import ImageDataGenerator
from keras.layers.normalization import BatchNormalization
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from keras import initializers, regularizers, constraints,optimizers
from keras.callbacks import ModelCheckpoint, LearningRateScheduler,TensorBoard
from keras.layers import Add,Input,Conv3D,Convolution3D,Dropout,UpSampling3D,Concatenate,MaxPooling3D,\
GlobalAveragePooling3D,Dense,GlobalMaxPooling3D,Lambda,Activation,Reshape,Permute, PReLU, Deconvolution3D

def NormActi(x,**kwargs):
    norm_func = kwargs.get('norm_func',None)
    activation_func = kwargs.get('activation_func',None)
    block_prefix = kwargs.get('block_prefix')
    block_suffix = kwargs.get('block_suffix')

    leaky_ratio = 0.15
    
    if norm_func:
        norm_tensor = norm_func(name='%s_norm_%s'%(block_prefix,block_suffix))(x)
    else:
        norm_tensor = x
    if activation_func:
        activation_tensor = activation_func(alpha=leaky_ratio,name='%s_activation_%s'%(block_prefix,block_suffix))(norm_tensor)
    else:
        activation_tensor = norm_tensor
    return activation_tensor

def ConvUnit(x,**kwargs):
    
    '''
    Todo: replace conv_func with Conv3D

    '''
    norm_func = kwargs.get('norm_func',None)
    activation_func = kwargs.get('activation_func',None)
    num_filters = kwargs.get('num_filters')
    kernel_size = kwargs.get('kernel_size',3)
    atrous_rate = kwargs.get('atrous_rate',1)
    conv_stride = kwargs.get('conv_stride',1)
    padding = kwargs.get('padding','valid')
    
    block_prefix = kwargs.get('block_prefix')
    layer_idx = kwargs.get('layer_idx',1)
    conv_first = kwargs.get('conv_first',True)
    kernel_initializer = kwargs.get('kernel_initializer','he_normal')
    kernel_regularizer = kwargs.get('kernel_regularizer',l2(1e-4))

    block_suffix = '%02d'%layer_idx
    activation_inner = 'relu'
    
    conv_func = Convolution3D(filters=num_filters,kernel_size=kernel_size,strides=conv_stride,dilation_rate=atrous_rate,
                              kernel_initializer=kernel_initializer,kernel_regularizer=kernel_regularizer,
                                   padding=padding,name='%s_conv_%s'%(block_prefix,block_suffix),activation=activation_inner)
    
    if conv_first:
        conv_tensor = conv_func(x)
        final_tensor = NormActi(conv_tensor,norm_func=norm_func,activation_func=activation_func,
                               block_prefix=block_prefix,block_suffix=block_suffix)
    else:
        norm_tensor = NormActi(x,norm_func=norm_func,activation_func=activation_func,
                               block_prefix=block_prefix,block_suffix=block_suffix)
        final_tensor = conv_func(norm_tensor)
    
    return final_tensor
    

def ASPP(x,**kwargs):
    basic_atrous_rate = kwargs.get('atrous_rate',2)

    norm_func = kwargs.get('norm_func')
    activation_func = kwargs.get('activation_func')
    num_filters = kwargs.get('num_filters')
    kernel_size = kwargs.get('kernel_size',3)
    padding = kwargs.get('padding','same')
    layer_idx = kwargs.get('layer_idx')

    block_idx = kwargs.get('block_idx',1)
    stride = kwargs.get('stride',1)
    conv_first = kwargs.get('conv_first',True)
    kernel_initializer = kwargs.get('kernel_initializer','he_normal')
    kernel_regularizer = kwargs.get('kernel_regularizer',l2(1e-4))
    # upsample_enlarge_ratio = kwargs.get('upsample_enlarge_ratio')

    
    block_prefix = 'ASPP_block%02d_%02d'%(block_idx,layer_idx)
    block_suffix = "flatten1x1"

    # flatten_feature = MaxPooling3D(upsample_enlarge_ratio,name='%s_GAP'%block_prefix)(x)
    # conv_feature = Convolution3D(filters=num_filters,kernel_size=1,kernel_initializer=kernel_initializer,kernel_regularizer=kernel_regularizer,
    #                                padding=padding,name='%s_conv_%s'%(block_prefix,block_suffix),activation=activation_func)(flatten_feature)
    # conv_feature = UpSampling3D(upsample_enlarge_ratio)(conv_feature)


    feature_1x1 = ConvUnit(x,norm_func=norm_func,activation_func=activation_func,num_filters=num_filters,
                    kernel_size=1,atrous_rate=1,padding=padding,block_prefix=block_prefix,
                     kernel_initializer=kernel_initializer,kernel_regularizer=kernel_regularizer,
                    layer_idx=1,conv_first=conv_first)
    feature_3x3_atrous0 = ConvUnit(x,norm_func=norm_func,activation_func=activation_func,num_filters=num_filters,
                    kernel_size=kernel_size,atrous_rate=basic_atrous_rate,padding=padding,block_prefix=block_prefix,
                     kernel_initializer=kernel_initializer,kernel_regularizer=kernel_regularizer,
                    layer_idx=2,conv_first=conv_first)
    feature_3x3_atrous1 = ConvUnit(x,norm_func=norm_func,activation_func=activation_func,num_filters=num_filters,
                    kernel_size=kernel_size,atrous_rate=basic_atrous_rate*2,padding=padding,block_prefix=block_prefix,
                     kernel_initializer=kernel_initializer,kernel_regularizer=kernel_regularizer,
                    layer_idx=3,conv_first=conv_first)
    feature_3x3_atrous2 = ConvUnit(x,norm_func=norm_func,activation_func=activation_func,num_filters=num_filters,
                    kernel_size=kernel_size,atrous_rate=basic_atrous_rate*3,padding=padding,block_prefix=block_prefix,
                     kernel_initializer=kernel_initializer,kernel_regularizer=kernel_regularizer,
                    layer_idx=4,conv_first=conv_first)
    
    return Concatenate(name='%s_Concatenate'%(block_prefix))([feature_1x1,feature_3x3_atrous0,feature_3x3_atrous1,feature_3x3_atrous2])
                    