import {
	Clock,
	Vector3
} from 'three';

import { PointerLockControls } from 'three/examples/jsm/Addons.js';

class PointerControls {
    constructor(camera, hero, domElement) {
        this.kb = {};

        this.canJump = false;
        this.velocity = new Vector3();
        this.direction = new Vector3();
        this.rotationY = 0;
        this.vertex = new Vector3();
        this.rvelo = 0;

        this.camera = camera;
        this.hero = hero;
        this.clock = new Clock();
        this.click = false;

        this.controls = new PointerLockControls(camera, domElement);

        const onKeyDown = (event) => {
            this.kb[event.code] = true;
            switch (event.code) {
                case 'Space':
                    if (this.canJump === true) this.velocity.y += 350;
                    this.canJump = false;
                    break;
            }
        };

        const onKeyUp = (event) => {
            this.kb[event.code] = false;
        };

        document.addEventListener('keydown', onKeyDown);
        document.addEventListener('keyup', onKeyUp);

        window.addEventListener("gamepadconnected", (e) => {
            this.gp = navigator.getGamepads()[e.gamepad.index];
            console.log("Gamepad connected", this.gp);
        });

        domElement.addEventListener('click', () => {
            this.controls.isLocked && this.clicked && this.clicked();
        });

        domElement.addEventListener('mousedown', () => {
            this.controls.isLocked && this.onpointer && this.onpointer('start');
        });

        domElement.addEventListener('mousemove', () => {
            this.controls.isLocked && this.onpointer && this.onpointer('drag');
        });

        domElement.addEventListener('mouseup', () => {
            this.controls.isLocked && this.onpointer && this.onpointer('end');
        });

        this.update = () => {
            // const delta = this.clock.getDelta() * 10;
            // if (this.gp) {
            //     this.gp = navigator.getGamepads()[this.gp.index];
            //     this.gp.pressed = this.gp.buttons[4].pressed || this.gp.buttons[5].pressed || this.gp.buttons[6].pressed || this.gp.buttons[7].pressed || this.gp.buttons[9].pressed;
            //     if (!this.click && this.gp.pressed) {
            //         this.click = true;
            //         this.clicked?.();
            //         //console.log(this.gp.buttons.map((b,i)=>[i, b.pressed]));
            //     }
            //     if (this.click && !this.gp.pressed) this.click = false;
            // }
            // //this.velocity.x -= this.velocity.x * 5.0 * delta;
            // this.velocity.z -= this.velocity.z * 5.0 * delta;
            // this.rvelo -= this.rvelo * 5 * delta;
            // // this.velocity.y -= 9.8 * 100.0 * delta; // 100.0 = mass
            // this.direction.z = Number(this.moveForward) - Number(this.moveBackward) - Math.floor((this.gp && this.gp.axes[1] || 0) * 100) / 100 + (this.touchControls && this.touchControls.move || 0);
            // this.rotationY = Number(this.rotateLeft) - Number(this.rotateRight) - Math.floor((this.gp && this.gp.axes[0] || 0) * 100) / 110 + (this.touchControls && this.touchControls.rotate || 0);
            // //this.direction.x = Number( this.moveRight ) - Number( this.moveLeft );
            // //this.direction.normalize(); // this ensures consistent movements in all directions
            // if (this.direction.z) this.velocity.z -= this.direction.z * 5.0 * delta;
            // //if ( this.moveLeft || this.moveRight ) this.velocity.x -= this.direction.x * 5.0 * delta;
            // if (this.rotationY) this.rvelo -= this.rotationY * 8 * delta;

            //this.velocity.x && this.controls.moveRight( - this.velocity.x * delta );
            // if (this.velocity.z) {
            //     this.controls.moveForward(-this.velocity.z * delta);
            //     this.hero.position.z += -this.velocity.z * delta;
            // }
            // this.controls.moveRight( this.direction.x * delta );
            // this.controls.moveForward( this.direction.z * delta );
            // this.hero.position.y += (Number(this.moveUp) - Number(this.moveDown)) * delta;
            // this.hero.rotation.y += -this.rvelo * delta;
        };

    }

    get moveForward(){
        return this.kb['ArrowUp'] || this.kb['KeyW'] || false
    }

    get moveLeft(){
        return this.kb['ArrowLeft'] || this.kb['KeyA'] || false
    }

    get rotateLeft(){
        return this.moveLeft;
    }

    get moveRight(){
        return this.kb['ArrowRight'] || this.kb['KeyD'] || false
    }

    get rotateRight(){
        return this.moveRight;
    }

    get moveBackward(){
        return this.kb['ArrowDown'] || this.kb['KeyS'] || false
    }

    get moveUp(){
        return this.kb['KeyR'] || false
    }

    get moveDown(){
        return this.kb['KeyF'] || false
    }

    get cameraLeft(){
        return this.kb['KeyQ'] || false
    }

    get cameraRight(){
        return this.kb['KeyE'] || false
    }

    get moving(){
        return this.moveForward || this.moveBackward;
    }

    get rotating(){
        return this.rotateLeft || this.rotateRight;
    }

    get motion(){
        return this.moving || this.rotating;
    }

    get running(){
        return this.moving && this.kb.ShiftLeft;
    }
}

export { PointerControls };