import * as THREE from 'three';
import { Hive } from './Hive.js';

import { LineMaterial } from 'three/examples/jsm/lines/LineMaterial.js';
import { LineGeometry } from 'three/examples/jsm/lines/LineGeometry.js';
import { Line2 } from 'three/examples/jsm/lines/Line2.js';

export class Waypoint {

    static collection = [];
    constructor(scene) {
        this.scene = scene;
        this.position = new THREE.Vector3();
        this.links = [];
        this.lines = [];
        this.special = null;
    }


    init() {
    }

    update(delta, timer) {

    }

    addLink(link) {
        this.links.push(link);
    }

    removeLink(link) {
        this.links.splice(this.links.indexOf(link), 1);
        return link;
    }

    rebuildLines() {
        for (let i = 0; i < this.lines.length; i++) {
            this.scene.remove(this.lines[i]);
        }
        this.lines.length = 0;
        for (let s = 0; s < this.links.length; s++) {
            let link = this.links[s];
            let line = Waypoint.buildLine(this, link);
            this.lines.push(line);
        }
    }

    addLine(link) {
        this.lines.push(Waypoint.buildLine(this, link));
    }

    removeLine(link) {
        this.rebuildLines();
    }

    contains(link) {
        return this.links.includes(link);
    }

    visitFromBee(bee) {
        if (bee.carryingHoney > 0) {
            bee.setTarget(this.links.filter(l => l instanceof Hive)[0]);
            return;
        }
        bee.setTarget(this.links[Math.floor(this.links.length * Math.random())]);
    }

    static addNewHive(scene, hive) {
        let newWaypoint = new Waypoint(scene);
        newWaypoint.position.x = hive.position.x + (30 * Math.cos((Math.PI / 2) - hive.rotation.y));
        newWaypoint.position.y = hive.position.y;
        newWaypoint.position.z = hive.position.z + (30 * Math.sin((Math.PI / 2) - hive.rotation.y));
        newWaypoint.addLink(hive);
        Waypoint.collection.push(newWaypoint);
        return newWaypoint;
    }

    static buildLine(startObj, endObj) {
        let points = [];
        points.push(new THREE.Vector3(startObj.position.x, 1.2, startObj.position.z));
        points.push(new THREE.Vector3(endObj.position.x, 1.2, endObj.position.z));
        let color = new THREE.Color();
        color.setHSL(Math.floor(360 * Math.random()) / 360, 1.0, 0.7);
        let positions = [];
        let colors = [];
        for (let w = 0; w < points.length; w++) {
            positions.push(points[w].x, points[w].y, points[w].z);

            colors.push(color.r, color.g, color.b);
        }
        let lineGeo = new LineGeometry();
        lineGeo.setPositions(positions);
        lineGeo.setColors(colors);
        let lineMat = new LineMaterial({

            color: 0xffffff,
            linewidth: 5, // in pixels
            vertexColors: true,
            //resolution:  // to be set by renderer, eventually
            dashed: false

        });

        lineMat.resolution.set(window.innerWidth, window.innerHeight);

        let line = new Line2(lineGeo, lineMat);
        line.computeLineDistances();
        line.scale.set(1, 1, 1);
        return line;
    }
}
