import { Column, Entity, JoinColumn, ManyToOne, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
import { PolygonExtremes, PolygonPayload } from '../features/polygon';
import { PrisonCellBlockEntity } from './prison-cell-block.entity';
import { FurnitureItemEntity } from './furniture-item.entity';
import { CharacterEntity } from './character.entity';

@Entity('prison_cells')
export class PrisonCellEntity {
    @PrimaryGeneratedColumn() id: number;
    @Column() cell_block_id: number;
    @Column({ type: 'varchar', length: 8 }) name: string;
    @Column({ type: 'tinyint', width: 1 }) decorable: boolean;
    @Column({ type: 'tinyint', width: 1 }) static: boolean;
    @Column({ type: 'json' }) polygon: {
        payload: PolygonPayload;
        extremes: PolygonExtremes;
    };
    @Column({ type: 'varchar', length: 32 }) door_object_hash: string;
    @Column({ type: 'float' }) door_closed_x: number;
    @Column({ type: 'float' }) door_closed_y: number;
    @Column({ type: 'float' }) door_closed_z: number;
    @Column({ type: 'float' }) door_closed_rx: number;
    @Column({ type: 'float' }) door_closed_ry: number;
    @Column({ type: 'float' }) door_closed_rz: number;
    @Column({ type: 'float' }) door_open_x: number;
    @Column({ type: 'float' }) door_open_y: number;
    @Column({ type: 'float' }) door_open_z: number;
    @Column({ type: 'float' }) door_open_rx: number;
    @Column({ type: 'float' }) door_open_ry: number;
    @Column({ type: 'float' }) door_open_rz: number;
    @Column({ type: 'float' }) spawn_x: number;
    @Column({ type: 'float' }) spawn_y: number;
    @Column({ type: 'float' }) spawn_z: number;
    @Column({ type: 'float' }) spawn_h: number;
    @Column({ type: 'int' }) layout_grid_x: number;
    @Column({ type: 'int' }) layout_grid_y: number;
    @Column({ type: 'int' }) floor: number;
    @Column({ type: 'boolean' }) free_furniture: boolean;

    @ManyToOne(() => PrisonCellBlockEntity, (cellBlock) => cellBlock.prisonCells)
    @JoinColumn({ name: 'cell_block_id' })
    prisonCellBlock: PrisonCellBlockEntity;

    @OneToMany(() => FurnitureItemEntity, (item) => item.prisonCell)
    @JoinColumn({ name: 'prison_cell_id' })
    furnitureItems: FurnitureItemEntity[];

    @OneToMany(() => CharacterEntity, (character) => character.prisonCell)
    @JoinColumn({ name: 'prison_cell_id' })
    residents: CharacterEntity[];

    object?: ObjectMp;
    label?: TextLabelMp;

    getFullName(): string {
        return `${this.prisonCellBlock.name}${this.name}`;
    }

    locked: boolean;

    _open: boolean;

    get open(): boolean {
        return this._open;
    }

    set open(value: boolean) {
        this._open = value;
        this.object?.setVariable('cellIsOpen', this._open);
    }
}
