import { Component, Input, OnInit, ChangeDetectorRef } from '@angular/core';
import { DialogRef, DialogContentBase } from '@progress/kendo-angular-dialog';
import { first } from 'rxjs/operators';
import { MD2MobLevelInfo } from '../../massive-darkness2.db.model';
import { MobSkillType } from '../../massive-darkness2.model.boss';
import { MD2MobLevelInfoService } from '../../service/massive-darkness2.service';
import { MD2Icon, MobType } from '../../massive-darkness2.model';

@Component({
    selector: 'ngx-md2-mob-level-editor',
    templateUrl: './md2-mob-level-editor.component.html',
    styleUrls: ['./md2-mob-level-editor.component.scss']
})
export class MD2MobLevelEditorComponent extends DialogContentBase implements OnInit {
    MobType = MobType;
    MD2Icon = MD2Icon;
    MobSkillType = MobSkillType;
    @Input() public data: MD2MobLevelInfo;
    @Input() public mobType: MobType;
    @Input() public mobInfoId: string;
    @Input() public isAdding: boolean = false;

    public model: MD2MobLevelInfo;
    public processing: boolean = false;

    constructor(
        public dialog: DialogRef,
        private mobLevelInfoService: MD2MobLevelInfoService,
        private cdr: ChangeDetectorRef
    ) {
        super(dialog);
    }

    ngOnInit(): void {
        this.initializeModel();
    }

    public initializeModel(): void {
        this.model = {
            id: this.data?.id || '',
            level: this.data?.level ?? 1,
            mobInfoId: this.mobInfoId || this.data?.mobInfoId || '',
            rewardTokens: this.data?.rewardTokens ?? 0,
            fixedRareTreasure: this.data?.fixedRareTreasure ?? 0,
            fixedEpicTreasure: this.data?.fixedEpicTreasure ?? 0,
            fixedLegendTreasure: this.data?.fixedLegendTreasure ?? 0,
            fixedHp: this.data?.fixedHp ?? 1,
            hpPerHero: this.data?.hpPerHero ?? 0,
            actions: this.data?.actions ?? 1,
            attackInfo: this.data?.attackInfo
                ? { ...this.data.attackInfo }
                : { type: MobSkillType.Attack, yellow: null, orange: null, red: null, blue: null, green: null, black: null },
            defenceInfo: this.data?.defenceInfo
                ? { ...this.data.defenceInfo }
                : { type: MobSkillType.Defense, yellow: null, orange: null, red: null, blue: null, green: null, black: null }
        };

        this.cdr.detectChanges();
    }

    public close(): void {
        this.dialog.close();
    }

    public save(): void {
        if (!this.processing) {
            this.processing = true;

            // Ensure required objects exist
            if (!this.model.attackInfo) {
                this.model.attackInfo = { type: MobSkillType.Attack, yellow: null, orange: null, red: null, blue: null, green: null, black: null };
            }
            if (!this.model.defenceInfo) {
                this.model.defenceInfo = { type: MobSkillType.Defense, yellow: null, orange: null, red: null, blue: null, green: null, black: null };
            }

            this.mobLevelInfoService.createOrUpdate(this.model).pipe(first()).subscribe(result => {
                this.processing = false;
                this.dialog.close(result);
            }, error => {
                this.processing = false;
                console.error('Error saving mob level info:', error);
            });
        }
    }

    public get isValid(): boolean {
        if (!this.model) {
            return false;
        }
        return this.model.level > 0 && this.model.mobInfoId !== '';
    }
}

