import OimoBodyData from "./OimoBodyData";
import OimoHelper from "./OimoHelper";
import { DealPackaging } from "../GameLogic/DealPackaging";

export default class OimoRigidBody extends Laya.Script3D {

    public constructor() {
        super();
    }

    public transform: Laya.Transform3D;
    public body: OIMO.RigidBody;
    public bodyData: OimoBodyData;
    public sprite: Laya.MeshSprite3D;
    public init(data: OimoBodyData): void {
        this.bodyData = data;
        this.sprite = this.owner as Laya.MeshSprite3D;
        this.transform = this.sprite.transform;

        data.type = DealPackaging.DealLayer(this.sprite.layer);
        this.initBodyData(data);
        if (this.body && this.body.parent) this.body.dispose();
        this.body = OimoHelper.Inst.createBody(data);
    }

    public onAwake(): void {

    }

    public onStart(): void {

    }

    public onUpdate(): void {
        let bodyPos = this.body.getPosition();
        let bodyQut = this.body.getQuaternion();
        let transPos = this.transform.position;
        let transQut = this.transform.rotation;
        transPos.x = bodyPos.x;
        transPos.y = bodyPos.y;
        transPos.z = bodyPos.z;
        this.transform.position = transPos;
        transQut.x = bodyQut.x;
        transQut.y = bodyQut.y;
        transQut.z = bodyQut.z;
        transQut.w = bodyQut.w;
        this.transform.rotation = transQut;
    }

    public checkContact(name) {
        if (this.body && this.body.contactLink) {
            let otherBody = this.body.contactLink.body;

            if (otherBody.name == name) return true;
        }

        return false;
    }

    public initBodyData(data: OimoBodyData) {
        data.type = data.type === undefined ? this.sprite.name : data.type;
        data.pos = this.getPos();
        data.rot = this.getRot();
        data.size = data.size === undefined ? this.getSize() : data.size;
        // if (data.type == "cube" || data.type == "barrier") data.type = "box";
        // else if (data.type == "capsule") data.type = "cylinder";
        // else if (data.type == "shell") data.type = "sphere";
        // else data.type = "box";
    }

    public getPos() {
        let position = this.transform.position;
        let pos = [position.x, position.y, position.z];
        return pos;
    }

    public getSize() {
        var w, h, d, size;
        var _scale = this.transform.scale;
        if (this.bodyData.type == "capsule" || this.bodyData.type == "cylinder") {
            var _scale = this.transform.scale;
            // w = this.transform.localScale.x;
            // h = this.transform.localScale.y * 2;
            // d = this.transform.localScale.z;
            w = _scale.x*0.5;
            h = _scale.y * 2;
            d = _scale.z*0.5;
            size = [w, h, d];
        } else if (this.bodyData.type == "sphere") {
            // w = this.transform.localScale.x;
            w = _scale.x*0.5;
            size = [w];
        } else if ( this.bodyData.type == "cube" || this.bodyData.type == "box") {
            // w = this.transform.localScale.x;
            // h = this.transform.localScale.y;//* 2;
            // d = this.transform.localScale.z;
            w = _scale.x;
            h = _scale.y;
            d = _scale.z;
            size = [w, h, d];
        }
      //  console.log(size);
        return size;
    }

    public getRot() {
        let euler = this.transform.rotationEuler;
        let rot = [euler.x, euler.y, euler.z];
        return rot;
    }
    public bodyName() {
        if (this.body) return this.body.name;
        return -1;
    }
}