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';
import { MD2Service } from '../../../../services/MD2/md2.service';

@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 selectedAttackType: { value: MobSkillType; text: string } | null = null;
    public model: MD2MobLevelInfo;
    public processing: boolean = false;
    public attackTypes: Array<{ value: MobSkillType; text: string }> = [];
    public selectedAlterAttackType: { value: MobSkillType; text: string } | null = null;
    constructor(
        public dialog: DialogRef,
        private mobLevelInfoService: MD2MobLevelInfoService,
        private cdr: ChangeDetectorRef,
        private md2Service: MD2Service
    ) {
        super(dialog);
    }

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

    public initializeEnums(): void {
        this.attackTypes = [
            { value: MobSkillType.Attack, text: 'None' },
            { value: MobSkillType.MeleeAttack, text: this.md2Service.iconHtml(MD2Icon.Melee) + ' Melee Attack' },
            { value: MobSkillType.RangeAttack, text: this.md2Service.iconHtml(MD2Icon.Range) + ' Range Attack' },
            { value: MobSkillType.MagicAttack, text: this.md2Service.iconHtml(MD2Icon.Magic) + ' Magic Attack' },
        ];
        this.selectedAttackType = this.attackTypes.find(t => t.value === this.model.attackInfo.type) || this.attackTypes[0] || null;
        this.selectedAlterAttackType = this.attackTypes.find(t => t.value === this.model.alterAttackInfo?.type) || this.attackTypes[0] || null;
    }
    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 },
            alterAttackInfo: this.data?.alterAttackInfo
                ? { ...this.data.alterAttackInfo }
                : { 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.model.attackInfo.type = this.selectedAttackType?.value ?? MobSkillType.Attack;
            if (this.model.alterAttackInfo) {
                this.model.alterAttackInfo.type = this.selectedAlterAttackType?.value ?? MobSkillType.Attack;
            }
            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 !== '';
    }
}

