#pragma once

#include "glm.hpp"
#include <glm/gtx/hash.hpp>

namespace glm {
    constexpr ivec3 inline iround(const vec3& p) {
        return ivec3(std::round<int>(p.x), std::round<int>(p.y), std::round<int>(p.z));
    }
    constexpr float inline length2(const vec3& a) {
        return a.x * a.x + a.y * a.y + a.z * a.z;
    }
    constexpr long inline length2(const lvec3& a) {
        return a.x * a.x + a.y * a.y + a.z * a.z;
    }
    constexpr long long inline length2(const llvec3& a) {
        return a.x * a.x + a.y * a.y + a.z * a.z;
    }
    constexpr long inline pow2(int v) {
        return v * v;
    }
    constexpr long long inline pow2(long v) {
        return v * v;
    }
    constexpr ivec3 inline diff(const ivec3& a, const ivec3& b) {
        return glm::abs(glm::abs(a) - glm::abs(b));
    }

    constexpr glm::u32 inline rem(long long value, glm::u32 m) {
        return value < 0 ? ((value+1) % (long long)m) + m - 1 : value % (long long)m;
    }
    constexpr long inline div(long long value, glm::u32 m) {
        return value < 0 ? ((value+1) / (long long)m) - 1 : value / (long long)m;
    }
    constexpr float inline div(float value, glm::u32 m) {
        return value < 0 ? ((value+1) / m) - 1 : value / m;
    }
    constexpr ucvec3 inline modulo(const llvec3& value, const ucvec3& m = ucvec3(IDX_LENGTH)) {
        return ucvec3(rem(value.x, m.x), rem(value.y, m.y), rem(value.z, m.z));
    }
    constexpr lvec3 inline divide(const llvec3 &value, const uvec3 &m) {
        return lvec3(div(value.x, m.x), div(value.y, m.y), div(value.z, m.z));
    }
    constexpr lvec3 inline divide(const llvec3 &value, const ucvec3 &m = ucvec3(IDX_LENGTH)) {
        return lvec3(div(value.x, m.x), div(value.y, m.y), div(value.z, m.z));
    }
    constexpr vec3 inline divide(const vec3 &value, const uvec3 &m) {
        return vec3(div(value.x, m.x), div(value.y, m.y), div(value.z, m.z));
    }
    constexpr llvec3 inline multiply(const lvec3 &value, const ucvec3 &m = ucvec3(IDX_LENGTH)) {
        return llvec3(value) * llvec3(m);
    }
    constexpr std::pair<lvec3, ucvec3> inline split(const llvec3 &value, const ucvec3 &m = ucvec3(IDX_LENGTH)) {
        return {divide(value, m), modulo(value, m)};
    }
    constexpr ucvec3 inline fromIdx(idx idx) {
        assert(idx < IDX_SIZE);
        return ucvec3(idx / IDX_LENGTH2, (idx / IDX_LENGTH) % IDX_LENGTH, idx % IDX_LENGTH);
    }
    constexpr idx inline toIdx(glm::u8 x, glm::u8 y, glm::u8 z) {
        return (x * IDX_LENGTH + y) * IDX_LENGTH + z;
    }
    constexpr idx inline toIdx(glm::u8 x, glm::u8 y, glm::u8 z, glm::u8 sy, glm::u8 sz) {
        return x * sy * sz + y * sz + z;
    }
    constexpr idx inline toIdx(ucvec3 pos) {
        return toIdx(pos.x, pos.y, pos.z);
    }
    constexpr idx inline toIdx(ucvec3 pos, ucvec3 size) {
        return toIdx(pos.x, pos.y, pos.z, size.y, size.z);
    }
    constexpr std::pair<lvec3, idx> inline splitIdx(const llvec3 &value, const ucvec3 &m = ucvec3(IDX_LENGTH)) {
        return {divide(value, m), toIdx(rem(value.x, m.x), rem(value.y, m.y), rem(value.z, m.z))};
    }
    template <typename T>
    constexpr T inline max_axis(const vec<3, T> &v) {
        return std::max({v.x, v.y, v.z});
    }
    template <typename T>
    ivec3 inline inormalize(const vec<3, T> &v) {
        const auto a = glm::abs(v);
        return a.x >= a.y ? (a.x >= a.z ? ivec3(glm::sign(v.x), 0, 0) : ivec3(0, 0, glm::sign(v.z))) :
                            (a.y >= a.z ? ivec3(0, glm::sign(v.y), 0) : ivec3(0, 0, glm::sign(v.z)));
    }
}
