import { Column, Entity, Index, JoinColumn, ManyToOne, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
import { ItemCategoryEntity } from './item-category.entity';
import { ItemEntity } from './item.entity';
import { PrescriptionEntity } from './prescription.entity';
import { DarknetOrderItemEntity } from './darknet-order-item.entity';
import { GenderString } from '../../shared/const/gender.const';
import { PropertyStoreStockEntity } from './property-store-stock.entity';
import { PropertyPermitTypeAvailableItemEntity } from './property-permit-type-available-item.entity';
import { FiringMode } from '../features/damage';

interface ItemTypeClothingData {
    componentId?: number;
    drawable: number;
    texture: number;
    palette: number;
    forced?: boolean;
    states?: {
        type: string;
        variations: {
            drawable: number;
            texture: number;
            palette: number;
        }[];
    }[];
}

export interface DrugDosage {
    name: string;
    amount: number;
    multiplier?: number;
    smoke?: {
        object: string;
        offset: [number, number, number, number, number, number];
        puffs: number;
    };
    drink?: {
        object: string;
        offset: [number, number, number, number, number, number];
        sips: number;
    };
    emote: string;
    onset: number;
}

@Entity('item_types')
export class ItemTypeEntity {
    @PrimaryGeneratedColumn() id: number;
    @Column() name: string;
    @Column({ type: 'varchar', nullable: true, length: 32 }) internal_name: string | null;
    @Column() image: string;
    @Column() is_weight_multiplied: number;
    @Column() amount_multiplier: number;
    @Column() weight: number;
    @Index()
    @Column()
    category_id: number;
    @Column() default_amount: number;
    @Column({ type: 'varchar', length: 16 }) amount_unit: string;
    @Column({ type: 'int', nullable: true }) wholesale_price: number;
    @Column({ type: 'int', nullable: true }) sales_tax_category: number;
    @Column({ type: 'json' }) default_item_data: any;
    @Column({ type: 'json' }) properties: {
        permission?: string;
        gameObject?: string;
        loseOnDeath?: true;
        clothes?: {
            male?: ItemTypeClothingData;
            female?: ItemTypeClothingData;
        };
        drug?: {
            substance: string;
            prescribable?: boolean;
            pressable?: boolean;
            dosage: DrugDosage[];
        };
        drugPackage?: {
            substances: {
                [name: string]: {
                    capacity: number;
                    image: string;
                    prop?: string;
                };
            };
        };
        pillMold?: {
            capacity: number;
        };
        chemical?: {
            color: string;
            colorStrength: number;
            antiCakingStrength: number;
            bindingStrength: number;
        };
        cookable?: boolean;
        spraycan?: boolean;
        camera?: {
            zoomValue: number;
        };
        lens?: {
            zoomValue: number;
        };
        cleaning?: boolean;
        breathalyser?: boolean;
        allowedItemCategories?: number[];
        childInventory?: boolean;
        inventoryCapacity?: number;
        lockable?: boolean;
        canBePlaced?: boolean;
        canBeThrown?: boolean;
        canBeHeld?:
        | {
            bone: number;
            offset: [number, number, number, number, number, number];
        }
        | boolean;
        radio?: {
            volume: number;
            radius: number;
        };
        portableRadio?: {
            slots: number;
        };
        weapon?: {
            weaponID: string;
            supportedAmmoItemTypes: number[];
            baseDamage: number;
            accurateDistance: number;
            maxDistance: number;
            fireModes?: FiringMode[];
        };
        ammo?: {
            damageMultiplier: number;
        };
        weaponAttachments?: {
            type: string;
            component?: {
                [weapon: string]: string;
            };
        };
        props?: {
            [key in GenderString]?: {
                propID: number;
                drawable: number;
                texture: number;
            };
        };
        fishing?: {
            rod?: {
                odds: number;
            };
            bait?: {
                oddIncreaser: number;
                oddsOfLosing: number;
            };
        };
        fish?: {
            popularity: number;
        };
        scenario?: string;
        anim?: string;
        food?: 'eat';
        license?: {
            vehicle: boolean;
        };
        tourniquet?: boolean;
        gauze?: boolean;
        handcuff?: boolean;
        ziptie?: boolean;
        makeup?: {
            overlayID: number;
            minOpacity: number;
            decayStep: number;
            applyStep: number;
            object: string;
            offset: [number, number, number, number, number, number];
            patterns: {
                index: number;
                color: number;
                name: string;
            }[];
        };
        makeupwipe: boolean;
        money: boolean;
        moneyPack: boolean;
        scratchoff?: {
            prizes: {
                amount: number;
                volume: number;
            }[];
            symbolsPrizeAmount: number;
            wordPrize: {
                word: string;
                amount: number;
            };
            theme: string;
        };
        lockpick: boolean;
        scratch_file: boolean;
        blankNote: boolean;
        postalDelivery: boolean;
        ticket: boolean;
        jerryCan: boolean;
        phone: boolean;
        armour: boolean;
    };

    test = {
        scratchoff: {
            prizes: [
                { amount: 10_000_000, volume: 6 },
                { amount: 750_000, volume: 12 },
                { amount: 50_000, volume: 60 },
                { amount: 10_000, volume: 310 },
                { amount: 2_000, volume: 1668 },
                { amount: 1000, volume: 18168 },
                { amount: 500, volume: 30388 },
                { amount: 400, volume: 30392 },
                { amount: 200, volume: 121176 },
                { amount: 100, volume: 801533 },
                { amount: 50, volume: 849583 },
                { amount: 40, volume: 1820658 },
                { amount: 30, volume: 3033925 },
                { amount: 0, volume: 7781005 },
            ],
            symbolsPrizeAmount: 500,
            wordPrize: {
                word: 'WINNER',
                amount: 1000,
            },
            theme: 'thewinner',
        },
    };

    @ManyToOne(() => ItemCategoryEntity, (category) => category.types, {})
    @JoinColumn({ name: 'category_id' })
    category: ItemCategoryEntity;

    @OneToMany(() => ItemEntity, (item) => item.type)
    items: ItemEntity[];

    @OneToMany(() => DarknetOrderItemEntity, (item) => item.itemType)
    darknetOrderItems: DarknetOrderItemEntity[];

    @OneToMany(() => PropertyStoreStockEntity, (stock) => stock.itemType)
    propertyStoreStock: PropertyStoreStockEntity[];

    @OneToMany(() => PropertyPermitTypeAvailableItemEntity, (pPTAI) => pPTAI.itemType)
    pPTAI: PropertyPermitTypeAvailableItemEntity[];

    @OneToMany(() => PrescriptionEntity, (prescription) => prescription.itemType)
    prescriptions: PrescriptionEntity[];
}
