/**
 * 
 *      S Y M B I O S I S
 * 
 *                     ___ by Lucien Loiseau (contact@metapixel.art)
 * 
 * This file contains the source code for the art generator featured in 
 * the project "symbiosis" (https://symbiosis.metapixel.art).
 */

// hash utility
!function () { function t(h, r) { var s = this instanceof t ? this : e; return s.reset(r), "string" == typeof h && h.length > 0 && s.hash(h), s !== this ? s : void 0 } var e; t.prototype.hash = function (t) { var e, h, r, s, i; switch (i = t.length, this.len += i, h = this.k1, r = 0, this.rem) { case 0: h ^= i > r ? 65535 & t.charCodeAt(r++) : 0; case 1: h ^= i > r ? (65535 & t.charCodeAt(r++)) << 8 : 0; case 2: h ^= i > r ? (65535 & t.charCodeAt(r++)) << 16 : 0; case 3: h ^= i > r ? (255 & t.charCodeAt(r)) << 24 : 0, h ^= i > r ? (65280 & t.charCodeAt(r++)) >> 8 : 0 }if (this.rem = 3 & i + this.rem, i -= this.rem, i > 0) { for (e = this.h1; ;) { if (h = 4294967295 & 11601 * h + 3432906752 * (65535 & h), h = h << 15 | h >>> 17, h = 4294967295 & 13715 * h + 461832192 * (65535 & h), e ^= h, e = e << 13 | e >>> 19, e = 4294967295 & 5 * e + 3864292196, r >= i) break; h = 65535 & t.charCodeAt(r++) ^ (65535 & t.charCodeAt(r++)) << 8 ^ (65535 & t.charCodeAt(r++)) << 16, s = t.charCodeAt(r++), h ^= (255 & s) << 24 ^ (65280 & s) >> 8 } switch (h = 0, this.rem) { case 3: h ^= (65535 & t.charCodeAt(r + 2)) << 16; case 2: h ^= (65535 & t.charCodeAt(r + 1)) << 8; case 1: h ^= 65535 & t.charCodeAt(r) }this.h1 = e } return this.k1 = h, this }, t.prototype.result = function () { var t, e; return t = this.k1, e = this.h1, t > 0 && (t = 4294967295 & 11601 * t + 3432906752 * (65535 & t), t = t << 15 | t >>> 17, t = 4294967295 & 13715 * t + 461832192 * (65535 & t), e ^= t), e ^= this.len, e ^= e >>> 16, e = 4294967295 & 51819 * e + 2246770688 * (65535 & e), e ^= e >>> 13, e = 4294967295 & 44597 * e + 3266445312 * (65535 & e), e ^= e >>> 16, e >>> 0 }, t.prototype.reset = function (t) { return this.h1 = "number" == typeof t ? t : 0, this.rem = this.k1 = this.len = 0, this }, e = new t, MurmurHash3 = t}();

// color utilities
const colorRGB = (red, green, blue) => {
    return {
        r: red,
        g: green,
        b: blue
    }
}

const colorHSV = (hue, saturation, value) => {
    return {
        h: hue,
        s: saturation,
        v: value
    }
}

const deadPixel = colorRGB(255, 255, 255)

const isPixelDead = (pixel) => {
    return (pixel.r == deadPixel.r && pixel.g == deadPixel.g && pixel.b == deadPixel.g);
}

const HSVtoRGB = (_h, _s, _v) => {
    let _r, _g, _b, _i, _f, _p, _q, _t;
    _i = Math.floor(_h * 6);
    _f = _h * 6 - _i;
    _p = _v * (1 - _s);
    _q = _v * (1 - _f * _s);
    _t = _v * (1 - (1 - _f) * _s);
    switch (_i % 6) {
        case 0: _r = _v, _g = _t, _b = _p; break;
        case 1: _r = _q, _g = _v, _b = _p; break;
        case 2: _r = _p, _g = _v, _b = _t; break;
        case 3: _r = _p, _g = _q, _b = _v; break;
        case 4: _r = _t, _g = _p, _b = _v; break;
        case 5: _r = _v, _g = _p, _b = _q; break;
    }
    return colorRGB(Math.round(_r * 255), Math.round(_g * 255), Math.round(_b * 255));
}

const RGBtoHSV = (r, g, b) => {
    let _max, _min, _d, _h, _s, _v;
    _max = Math.max(r, g, b)
    _min = Math.min(r, g, b)
    _d = _max - _min
    _s = (_max === 0 ? 0 : _d / _max)
    _v = _max / 255

    switch (_max) {
        case _min: _h = 0; break;
        case r: _h = (g - b) + _d * (g < b ? 6 : 0); _h /= 6 * _d; break;
        case g: _h = (b - r) + _d * 2; _h /= 6 * _d; break;
        case b: _h = (r - g) + _d * 4; _h /= 6 * _d; break;
    }

    return colorHSV(_h, _s, _v);
}

const generateColor = (hue, brightness) => {
    return HSVtoRGB(hue, 0.8, brightness)
}


const generateGrey = (brightness) => {
    return HSVtoRGB(0, 0, brightness)
}

// pixel utilities
const getOffset = (w, h, x, y) => {
    return w * 4 * y + x * 4
}

const getPixel = (img, w, h, x, y) => {
    if (x < 0 || x >= w || y < 0 || y >= h) {
        return deadPixel
    }

    let offset = getOffset(w, h, x, y)
    let r = img[offset]
    let g = img[offset + 1]
    let b = img[offset + 2]

    return colorRGB(r, g, b)
}

const drawPixel = (img, w, h, x, y, rgb = colorRGB(0, 0, 0)) => {
    let offset = getOffset(w, h, x, y)
    img[offset] = rgb.r
    img[offset + 1] = rgb.g
    img[offset + 2] = rgb.b
    img[offset + 3] = 255
}

// lorenz attractor utilities
function LorenzParameters(c, h, scale, color, cx, cy, dx, dy) {
    this.c = c;
    this.h = h;
    this.scale = scale;
    this.color = color;
    this.cx = cx;
    this.cy = cy;
    this.dx = dx;
    this.dy = dy;
}

function LorenzAttractor(width, height, params) {
    this.width = width
    this.height = height
    this.c = params.c
    this.h = params.h
    this.t = -6
    this.x0 = 0
    this.y0 = 1
    this.z0 = 10
    this.dx = params.dx
    this.dy = params.dy
    this.cx = params.cx
    this.cy = params.cy
    this.scale = params.scale
    this.color = params.color
}

LorenzAttractor.prototype.next = function (img) {
    let x1, y1, z1
    for (let k = 0; k < 100; k += 1) {
        x1 = this.x0 + this.h * this.t * (this.x0 - this.y0);
        y1 = this.y0 + this.h * (-1 * this.x0 * this.z0 + this.c * this.x0 - this.y0);
        z1 = this.z0 + this.h * (this.x0 * this.y0 - this.z0);

        this.x0 = x1
        this.y0 = y1
        this.z0 = z1

        let lorenz_x, lorenz_y
        lorenz_x = Math.floor(this.cx + this.dx * this.x0 * this.scale)
        lorenz_y = Math.floor(this.cy + this.dy * this.y0 * this.scale)
        drawPixel(img, this.width, this.height, lorenz_x, lorenz_y, this.color)
    }
}

function Traits(under, over, gradual, grey) {
    this.under = under
    this.over = over
    this.gradual = gradual
    this.grey = grey
}

function OwnerDetail(publicKey, timestamp) {
    this.pk = publicKey
    this.timestamp = timestamp
}

OwnerDetail.prototype.toTrait = function (tokenId) {
    let seed = tokenId + this.pk + this.timestamp
    let hash = MurmurHash3(seed)
    let num1 = hash.hash("one").result() % 8 + 1
    let num2 = hash.hash("two").result() % 8 + 1
    let gradual = hash.hash("gradual").result() % 2
    let grey = hash.hash("grey").result() % 4 == 0
    return new Traits(num1 <= num2 ? num1 : num2, num1 <= num2 ? num2 : num1, gradual, grey)
}

OwnerDetail.prototype.toLorenzParameters = function (w, h, tokenId, t) {
    let seed = tokenId + this.pk + this.timestamp
    let hash = MurmurHash3(seed)
    let _c = (hash.hash("c").result() % 26 + 25)
    let _h = (hash.hash("h").result() % 10 + 1) * 0.0001
    let _scale = hash.hash("scale").result() % 4 + 2
    let _color
    if (t.grey) {
        _color = generateGrey((hash.hash("grey").result() % 100) / 100)
    } else {
        const hue = ((hash.hash("hue").result() % 100) / 100)
        const bright = ((hash.hash("bright").result() % 100) / 100)
        _color = generateColor(hue, bright)
    }
    let _cx = hash.hash("x").result() % w
    let _cy = hash.hash("y").result() % h
    let _dx = (hash.hash("dx").result() % 2 == 0 ? 1 : -1)
    let _dy = (hash.hash("dy").result() % 2 == 0 ? 1 : -1)
    return new LorenzParameters(_c, _h, _scale, _color, _cx, _cy, _dx, _dy)
}

function Generator(width, height, tokenId, ownerList) {
    this.w = width
    this.h = height
    this.tokenId = tokenId
    this.ownerList = ownerList.map(entry => new OwnerDetail(entry[0],entry[1]))
    this.numberOfSecondPerFrame = 43200 // 3600 * 12 
    this.state = {
        image: new Uint8ClampedArray(this.w * this.h * 4),
        events: ownerList.slice(),
        timestamp: 0,
        lorenzSet: [],
    }
    this.init()
}

Generator.prototype.init = function () {
    this.state.image.fill(255)
    this.state.events = this.ownerList.slice()
    this.state.lorenzSet = []

    let firstOwner = this.state.events.shift()
    this.state.traits = firstOwner.toTrait(this.tokenId)
    this.addOwner(firstOwner)

    firstOwner.timestamp -= this.numberOfSecondPerFrame*60;
    this.state.startTimestamp = firstOwner.timestamp
    this.state.timestamp = this.state.startTimestamp;
    this.state.frame = 0
}

Generator.prototype.conwayNextState = function () {
    let newImg = new Uint8ClampedArray(this.w * this.h * 4);
    let lca = this.state.lorenzSet[this.state.lorenzSet.length - 1].color
    for (let xt = 0; xt < this.w; xt++) {
        for (let yt = 0; yt < this.h; yt++) {
            let y = yt - 1
            let x = xt
            let currentColor = getPixel(this.state.image, this.w, this.h, x, y)
            let isAlive = !isPixelDead(currentColor)
            let neighbours = 0

            for (let xoff = -1; xoff <= 1; xoff++) {
                for (let yoff = -1; yoff <= 1; yoff++) {
                    let _color = getPixel(this.state.image, this.w, this.h, x + xoff, y + yoff)
                    if (!isPixelDead(_color)) {
                        neighbours++
                    }
                }
            }

            if (isAlive) {
                if (neighbours < this.state.traits.under || neighbours > this.state.traits.over) {
                    if (this.state.traits.gradual) {
                        let color = RGBtoHSV(currentColor.r, currentColor.g, currentColor.b)
                        let shaded = HSVtoRGB(color.h, color.s, color.v * 1.05)
                        lca = shaded
                        drawPixel(newImg, this.w, this.h, xt, yt, shaded)
                    } else {
                        drawPixel(newImg, this.w, this.h, xt, yt, deadPixel)
                    }
                } else {
                    drawPixel(newImg, this.w, this.h, xt, yt, currentColor)
                }
            } else {
                if (neighbours == 3) {
                    let color = RGBtoHSV(lca.r, lca.g, lca.b)
                    let shaded = HSVtoRGB(color.h, color.s / 1.05, color.v)
                    lca = shaded
                    drawPixel(newImg, this.w, this.h, xt, yt, shaded)
                } else {
                    drawPixel(newImg, this.w, this.h, xt, yt, deadPixel)
                }
            }

            if (yt <= 1) {
                drawPixel(newImg, this.w, this.h, xt, yt, deadPixel)
            }
        }
    }
    return newImg
}

Generator.prototype.addOwner = function (ownerDetail) {
    this.state.lorenzSet.push(new LorenzAttractor(this.w, this.h, ownerDetail.toLorenzParameters(this.w, this.h, this.tokenId, this.state.traits)))
}

Generator.prototype.nextFrame = function (maxFrame) {
    this.state.timestamp += Math.min(this.numberOfSecondPerFrame, maxFrame - this.state.timestamp)

    // add any owner that was added in the last sequence
    while (this.state.events.length > 0 && this.state.events[0].timestamp <= this.state.timestamp) {
        this.addOwner(this.state.events.shift())
    }

    // create a new frame
    if ((this.state.timestamp - this.state.startTimestamp) >= (this.numberOfSecondPerFrame * (this.state.frame + 1))) {
        this.state.frame++
        let nextFrameImage = this.conwayNextState()
        this.state.lorenzSet.forEach(lorenz => { lorenz.next(nextFrameImage) });
        this.state.image = nextFrameImage
    }

    // update image
    return this.state.image
}

Generator.prototype.getTimestamp = function () {
    return this.state.timestamp
}

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
    module.exports = { Generator };
}