# ======================================================================================================================
#  fennec, a free and open source game engine
#  Copyright © 2025 - 2026  Medusa Slockbower
#
#  This program 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
#  (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 General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <https://www.gnu.org/licenses/>.
# ======================================================================================================================
import os

import gdb
from . import utility

# VECTOR =================================================================================================================
class VectorPrinter:
    def __init__(self, val):
        self.val  = val
        self.base = val['data']['data']
        self.len  = self.base.type.range()[1] + 1

    def to_string(self):
        res = "< "
        if self.len > 0:
            res += "x = "   + str(self.val['x'])
        if self.len > 1:
            res += ", y = " + str(self.val['y'])
        if self.len > 2:
            res += ", z = " + str(self.val['z'])
        if self.len > 3:
            res += ", w = " + str(self.val['w'])
        res += " >"
        return res

    def children(self):
        return (('[{}]'.format(i), self.base[i]) for i in range(self.len))

    def display_hint(self):
        return 'array'


# PATH =================================================================================================================
class QuaternionPrinter:
    def __init__(self, val):
        self.val  = val
        self.base = val['data']['data']

    def to_string(self):
        res = ("< "
            + str(self.val['x']) + " i + "
            + str(self.val['y']) + " j + "
            + str(self.val['z']) + " k + "
            + str(self.val['w']))
        res += " >"
        return res

    def children(self):
        return (('[{}]'.format(i), self.base[i]) for i in range(4))

    def display_hint(self):
        return 'array'


# VECTOR =================================================================================================================
class MatrixPrinter:
    def __init__(self, val):
        self.columns     = val['data']['data']
        self.num_columns = self.columns.type.range()[1] + 1
        self.num_rows    = val.type.template_argument(1)

    def to_string(self):
        return "{ rows = " + str(self.num_rows) + ", columns = " + str(self.num_columns) + " }"

    def children(self):
        return (('[{}]'.format(i), self.columns[i]) for i in range(self.num_columns))

    def display_hint(self):
        return 'array'


# GDB Code =============================================================================================================

def register_printers():
    print("registering filesystem")
    pp = gdb.printing.RegexpCollectionPrettyPrinter("fennec::math")
    pp.add_printer('fennec::vector',     '^fennec::vector<.*>$',     VectorPrinter)
    pp.add_printer('fennec::quaternion', '^fennec::quaternion<.*>$', QuaternionPrinter)
    pp.add_printer('fennec::matrix',     '^fennec::matrix<.*>$',     MatrixPrinter)
    return pp

printer = register_printers()