from paraview.util.vtkAlgorithm import *
import vtk


@smproxy.source(name="DummySource",
       label="Dummy Source with blanked cells")
class DummySource(VTKPythonAlgorithmBase):
    """This is dummy uniform grid source."""

    _default_shape = [10]*3

    def __init__(self):
        VTKPythonAlgorithmBase.__init__(
            self, nInputPorts=0, nOutputPorts=1, outputType="vtkUniformGrid"
        )
        self._shape = list(DummySource._default_shape)

    @smproperty.intvector(name="VoxelShape", default_values=_default_shape)
    def SetVoxelShape(self, nx, ny, nz):
        self._shape = [n if n > 0 else 1 for n in [nx, ny, nz]]
        self.Modified()

    def RequestInformation(self, request, inInfoVec, outInfoVec):
        executive = self.GetExecutive()
        outInfo = outInfoVec.GetInformationObject(0)
        nx, ny, nz = self._shape
        outInfo.Set(executive.WHOLE_EXTENT(), 0, nx, 0, ny, 0, nz)
        vtkDO = vtk.vtkDataObject
        outInfo.Set(vtkDO.SPACING(), 1 / nx, 1 / ny, 1 / nz)
        outInfo.Set(vtkDO.ORIGIN(), 0, 0, 0)
        outInfo.Set(vtk.vtkAlgorithm.CAN_PRODUCE_SUB_EXTENT(), 1)
        return 1

    def RequestData(self, request, inInfoVec, outInfoVec):
        output = vtk.vtkUniformGrid.GetData(outInfoVec, 0)
        output.Initialize()
        nx, ny, nz = self._shape
        output.SetExtent(0, nx, 0, ny, 0, nz)
        output.SetDimensions(nx + 1, ny + 1, nz + 1)
        output.SetSpacing(1 / nx, 1 / ny, 1 / nz)
        output.SetOrigin(0.0, 0.0, 0)
        data = output.GetCellData()
        test = vtk.vtkTypeUInt16Array()
        test.SetNumberOfComponents(1)
        test.Resize(nx * ny * nz)
        test.SetName("k index")
        data.AddArray(test)
        for k in range(nz):
            for _ in range(nx * ny):
                test.InsertNextTuple1(k)
        nb_cells = output.GetNumberOfCells()
        for i in range(nb_cells//2):
            output.BlankCell(i)
        return 1
