import { Column, Entity, JoinColumn, ManyToOne, OneToOne, PrimaryGeneratedColumn } from 'typeorm';
import { InventoryEntity } from './inventory.entity';
import { PropertyEntity } from './property.entity';
import { PrisonCellEntity } from './prison-cell.entity';
import { ColumnDecimalTransformer } from '../features/utility/ColumnDecimalTransformer';
import { BlackjackGame } from '../features/gambling/games/blackjack/game';
import { Mutex } from 'async-mutex';
import MutexInterface from 'async-mutex/lib/MutexInterface';
import { RouletteGame, RouletteGameSettings } from '../features/gambling/games/roulette/game';

@Entity('furniture_items')
export class FurnitureItemEntity {
    @PrimaryGeneratedColumn() id: number;
    @Column({ type: 'varchar', length: 100 }) name: string;
    @Column({ type: 'varchar', length: 100 }) hash: string;
    @Column({ type: 'varchar', length: 32, nullable: true }) tag: string | null;
    @Column({ type: 'int', nullable: true }) property_id: number | null;
    @Column({ type: 'int', nullable: true }) prison_cell_id: number | null;
    @Column() dimension: number;
    @Column({ type: 'decimal', precision: 17, scale: 12, transformer: new ColumnDecimalTransformer() }) x: number;
    @Column({ type: 'decimal', precision: 17, scale: 12, transformer: new ColumnDecimalTransformer() }) y: number;
    @Column({ type: 'decimal', precision: 17, scale: 12, transformer: new ColumnDecimalTransformer() }) z: number;
    @Column({ type: 'decimal', precision: 17, scale: 12, transformer: new ColumnDecimalTransformer() }) rx: number;
    @Column({ type: 'decimal', precision: 17, scale: 12, transformer: new ColumnDecimalTransformer() }) ry: number;
    @Column({ type: 'decimal', precision: 17, scale: 12, transformer: new ColumnDecimalTransformer() }) rz: number;
    @Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' }) created: Date;
    @Column() price: number;
    @Column() created_by_character: number;
    @Column({ type: 'boolean', default: false }) has_inventory: boolean;
    @Column({ type: 'boolean', default: false }) is_locked: boolean;
    @Column({ type: 'boolean', default: false }) is_tv: boolean;

    @Column({ type: 'json' }) data: {
        collision?: boolean;
        games?: {
            till: number;
            blackjack?: {
                enabled: boolean;
                packsInDeck: number;
                minBet: number;
                maxBet: number;
                hitOnSoft17: boolean;
                bjPayoutRatio: string;
                practiceMode: boolean;
                uiBackgroundImage?: string;
                decisionTimeout: number;
            };
            roulette?: RouletteGameSettings;
        };
    };

    @OneToOne(() => InventoryEntity)
    @JoinColumn({
        name: 'id',
        referencedColumnName: 'game_entity_id',
    })
    inventory?: InventoryEntity;

    @ManyToOne(() => PropertyEntity, (type) => type.furnitureItems)
    @JoinColumn({ name: 'property_id' })
    property: PropertyEntity;

    @ManyToOne(() => PrisonCellEntity, (type) => type.furnitureItems)
    @JoinColumn({ name: 'prison_cell_id' })
    prisonCell: PrisonCellEntity;

    get position(): Vector3 {
        return new mp.Vector3(this.x, this.y, this.z);
    }

    object?: ObjectMp;

    blackjackGame?: BlackjackGame;
    rouletteGame?: RouletteGame;

    tillMutex?: MutexInterface;

    isGameActive() {
        if (this.blackjackGame) {
            return true;
        }

        if (this.rouletteGame) {
            if (this.data.games?.roulette?.enabled) {
                return true;
            }

            if (this.rouletteGame.getPlayers().length > 0) {
                return true;
            }
        }

        return false;
    }
}
