"""
Copyright 2022  Salvatore Barone <salvatore.barone@unina.it>
                Filippo Ferrandino <fi.ferrandino@studenti.unina.it>

This is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3 of the License, or any later version.

This is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.

You should have received a copy of the GNU General Public License along with
RMEncoder; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
import numpy as np
from numba import cuda
from .BaseLayer import BaseLayer
from .cuda_utils import convolve

class ConvLayer(BaseLayer):
    def __init__(self, stride = (1, 1), padding = (0,0), activation = "relu", quant_nbits = 8, multiplier = BaseLayer.default_multiplier, name = "layer",offset=[128,128]):
        super().__init__(activation, quant_nbits, multiplier, name)
        self.stride = stride
        self.padding = padding
        self.kernel_global_mem = None
        self.M = None
        self.offset = offset
        
    def __deepcopy__(self, memo = None):
        return ConvLayer(stride = self.stride, padding = self.padding, activation = self.activation, quant_nbits = self.quant_nbits, multiplier = self.multiplier, name = self.name)

    def forward_pass(self, **kwargs):
        if self.gpu_input_memory is None:
            inputv = kwargs["inputv"]
            A_global_mem = cuda.to_device(np.array(inputv))
        else:
            A_global_mem = self.gpu_input_memory 
        convolve[self.griddim, self.blockdim](self.results, A_global_mem,self.kernel_global_mem,self.biases,self.n_channels,self.M,self.offset[0],self.offset[1],self.quant_nbits)
        cuda.synchronize()
        if self.gpu_output_memory == False:
            self.outputv[:,:,:] = self.results.copy_to_host()
            return self.outputv
  
    def load_weights(self, **kwargs):
        self.enable_gpu = kwargs["enable_gpu"]
        self.input_shape= kwargs["input_shape"]
        self.kernel_shape = np.shape(kwargs["weights"])        
        self.output_shape = [int(np.floor((self.input_shape[i] - self.kernel_shape[i] + 2 * self.padding[i]) / self.stride[i] + 1)) for i in range(2)] + [self.kernel_shape[3]]
        self.griddim = (self.input_shape[0] // self.blockdim[0] + 1, self.input_shape[1] // self.blockdim[1] + 1)
        self.n_channels=self.kernel_shape[3]
        if self.enable_gpu:
            self.use_gpu=True
            self.M = self.multiplier
            self.kernel_global_mem = cuda.to_device(np.array(kwargs["weights"]))
            self.biases = cuda.to_device(np.array(kwargs["biases"]))
            self.results = cuda.device_array(self.output_shape)
            self.gpu_input_memory = kwargs["gpu_input_memory"]
        else:
            self.convkernel, self.biases =kwargs["weights"], kwargs["biases"]
            self.outputv = np.zeros(self.output_shape)
        if self.gpu_input_memory is None:
                self.outputv = np.zeros(self.output_shape)
        return self.output_shape
  