import { Column, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import { PropertyEntity } from './property.entity';

@Entity('point_to_points')
export class PointToPointEntity {
    @PrimaryGeneratedColumn() id: number;
    @Column({ type: 'varchar', length: 64 }) name: string;
    @Column({ type: 'varchar', length: 128, nullable: true }) permission: string | null;
    @Column({ type: 'int', nullable: true, default: null }) property_id: number | null;
    @Column({ type: 'float' }) enter_x: number;
    @Column({ type: 'float' }) enter_y: number;
    @Column({ type: 'float' }) enter_z: number;
    @Column({ type: 'float' }) enter_h: number;
    @Column({ type: 'int' }) enter_dimension: number;
    @Column({ type: 'int' }) enter_radius: number;
    @Column({ type: 'float', nullable: true, default: null }) exit_x: number | null;
    @Column({ type: 'float', nullable: true, default: null }) exit_y: number | null;
    @Column({ type: 'float', nullable: true, default: null }) exit_z: number | null;
    @Column({ type: 'float', nullable: true, default: null }) exit_h: number | null;
    @Column({ type: 'int', nullable: true, default: null }) exit_dimension: number | null;
    @Column({ type: 'int', nullable: true, default: null }) exit_radius: number | null;
    @Column({ type: 'tinyint', width: 1 }) locked: boolean;
    @Column({ type: 'json' }) linked_points: number[];

    @ManyToOne(() => PropertyEntity)
    @JoinColumn({
        name: 'property_id',
        referencedColumnName: 'id',
    })
    property?: PropertyEntity;

    propertyColShape?: ColshapeMp;

    get enter(): Vector3 {
        return new mp.Vector3(this.enter_x, this.enter_y, this.enter_z);
    }

    get exit(): Vector3 | null {
        return this.exit_x === null ? null : new mp.Vector3(this.exit_x!, this.exit_y!, this.exit_z!);
    }

    isElevator(): boolean {
        return this.linked_points.length > 0;
    }
}
