# 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 itertools import chain, product
from math import tau, cos, sin, copysign
from array import array

from engine import TEXTURE_FORMAT_RGB10_A2, TEXTURE_FORMAT_TYPECODE, TEXTURE_FLAG_MIN_LINEAR, TEXTURE_FLAG_MAG_LINEAR

from game.math import vec3_scale, vec3_direction, vec3_cross, vec3_normal_rgb10a2
from game.texture import Texture
from game.triangles import Triangles
from game.resources import load_png

_format = TEXTURE_FORMAT_RGB10_A2
_typecode = TEXTURE_FORMAT_TYPECODE[TEXTURE_FORMAT_RGB10_A2]
_conv = vec3_normal_rgb10a2
_flags = TEXTURE_FLAG_MIN_LINEAR | TEXTURE_FLAG_MAG_LINEAR

def load_polar_textures(paths, waves_height = 0.008):
    def load_texture(_path):
        width, height, data = load_png(_path)
        assert data.typecode == 'H'
        assert len(data) == width * height
        def polar(_y, _x, _h):
            d = 1.0 + (_y / height)
            a = (_x / width) * tau
            return (d * sin(a), d * cos(a), (_h / 65535.0) * waves_height)
        def normal(_pos, _h):
            y, x = _pos
            o = polar(y, x, _h)
            n = vec3_cross(
                vec3_direction(o, polar(y, x + 1, data[y * width + ((x + 1) % width)])),
                vec3_direction(o, polar(y + 1, x, data[((y + 1) % height) * width + x])))
            return vec3_scale(n, copysign(1.0, n[2]))
        return (width, height, list(map(normal, product(range(height), range(width)), data)))
    width, height, normals = load_texture(paths[0])
    data = array(_typecode, list(map(_conv, normals)))
    for path in paths[1:]:
        _width, _height, normals = load_texture(path)
        assert _width == width and _height == height
        data.extend(list(map(_conv, normals)))
    return Texture(_format, width, height, len(paths), _flags, data)

def load_detail_texture(path, scale = 0.5, waves_height = 0.002):
    width, height, data = load_png(path)
    assert data.typecode == 'H'
    assert len(data) == width * height
    def coord(_y, _x, _h):
        return ((_x / width) * scale, (_y / height) * scale, (_h / 65535.0) * waves_height)
    def normal(_pos, _h):
        y, x = _pos
        o = coord(y, x, _h)
        n = vec3_cross(
            vec3_direction(o, coord(y, x + 1, data[y * width + ((x + 1) % width)])),
            vec3_direction(o, coord(y + 1, x, data[((y + 1) % height) * width + x])))
        return vec3_scale(n, copysign(1.0, n[2]))
    normals = list(map(normal, product(range(height), range(width)), data))
    data = array(_typecode, list(map(_conv, normals)))
    return Texture(_format, width, height, 0, _flags, data)

# TODO: with FOV
def sea_triangles(vsubdivs, distance, projection_ratio):
    assert vsubdivs > 0
    vertices = []
    hsubdivs = round(vsubdivs * projection_ratio)
    z = -distance
    width = distance * projection_ratio
    height = distance
    startx = width * -0.5
    starty = height * -0.5
    stepx = width / hsubdivs
    stepy = height / vsubdivs
    y1 = starty
    y2 = y1 + stepy
    for sy in range(vsubdivs):
        x1 = startx
        x2 = x1 + stepx
        for sx in range(hsubdivs):
            a = (x1, y2, z)
            b = (x2, y1, z)
            vertices.append((x1, y1, z))
            vertices.append(b)
            vertices.append(a)
            vertices.append((x2, y2, z))
            vertices.append(a)
            vertices.append(b)
            x1 = x2
            x2 += stepx
        y1 = y2
        y2 += stepy
    return Triangles(array('f', chain.from_iterable(vertices)))
