# Copyright (C) 2022 RozK
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from ctypes import c_ubyte, c_ushort, c_void_p, POINTER, addressof

from engine import (
    buffer, INSTANCE_FLAG_SPAWNED, BATCH_MAX_SIZE, vertex_type, vertex_format,
    create_batch, fill_batch, draw_batch, destroy_batch)

_flags_t = c_ubyte
_flags_p = POINTER(_flags_t)
_mesh_t = c_ushort
_mesh_p = POINTER(_mesh_t)

class Batch:
    __slots__ = ('_batch', 'vertices', 'max_size', 'size',
        'flags', '_flags', 'mesh', '_meshes', 'param', '_params', '_name', '__dict__')

    def __init__(self, vertices, max_size, **params_decls):
        assert max_size <= BATCH_MAX_SIZE
        nparams = len(params_decls)
        if nparams:
            self._batch = create_batch(vertices._vertices, max_size, vertex_format(*params_decls.values()))
        else:
            self._batch = create_batch(vertices._vertices, max_size, None)
        self.vertices = vertices
        self.max_size = max_size
        self.size = 0
        self.flags = buffer(_flags_t, max_size)
        self._flags = _flags_p(self.flags)
        self.mesh = buffer(_mesh_t, max_size)
        self._meshes = _mesh_p(self.mesh)
        if nparams:
            self.param = tuple(map(lambda f: buffer(vertex_type(f), max_size), params_decls.values()))
            self._params = (c_void_p * nparams)(*map(addressof, self.param))
            self._name = dict(zip(params_decls.keys(), range(nparams)))
            for name, value in zip(params_decls.keys(), self.param):
                setattr(self, name, value)
        else:
            self.param = None
            self._params = None
            self._name = None

    def __del__(self):
        destroy_batch(self._batch)

    def spawn(self, flags, mesh, **params):
        index = self.size
        assert index < self.max_size
        self.flags[index] = flags | INSTANCE_FLAG_SPAWNED
        self.mesh[index] = mesh
        for name, value in params.items():
            getattr(self, name)[index] = value
        self.size += 1
        return index

    def freeze(self, flags = None, mesh = None, **params):
        if flags is not None:
            self._flags = None if flags else _flags_p(self.flags)
        if mesh is not None:
            self._meshes = None if mesh else _mesh_p(self.mesh)
        for name, value in params.items():
            if value is not None:
                index = self._name[name]
                self._params[index] = None if value else addressof(self.param[index])

    def fill(self):
        fill_batch(self._batch, self.size, self._flags, self._meshes, self._params)

    def draw(self):
        fill_batch(self._batch, self.size, self._flags, self._meshes, self._params)
        draw_batch(self._batch)
