import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { mergeMap, shareReplay, take } from 'rxjs/operators';
import {
    ConfigurableOperation,
    ConfigurableOperationInput,
    CreatePromotionInput,
    FacetWithValues,
    LanguageCode,
    Promotion,
    UpdatePromotionInput,
} from 'shared/generated-types';

import { BaseDetailComponent } from '../../../common/base-detail.component';
import { _ } from '../../../core/providers/i18n/mark-for-extraction';
import { NotificationService } from '../../../core/providers/notification/notification.service';
import { DataService } from '../../../data/providers/data.service';
import { ServerConfigService } from '../../../data/server-config';

@Component({
    selector: 'vdr-promotion-detail',
    templateUrl: './promotion-detail.component.html',
    styleUrls: ['./promotion-detail.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class PromotionDetailComponent extends BaseDetailComponent<Promotion.Fragment>
    implements OnInit, OnDestroy {
    promotion$: Observable<Promotion.Fragment>;
    detailForm: FormGroup;
    conditions: ConfigurableOperation[] = [];
    actions: ConfigurableOperation[] = [];
    facets$: Observable<FacetWithValues.Fragment[]>;

    private allConditions: ConfigurableOperation[];
    private allActions: ConfigurableOperation[];

    constructor(
        router: Router,
        route: ActivatedRoute,
        serverConfigService: ServerConfigService,
        private changeDetector: ChangeDetectorRef,
        private dataService: DataService,
        private formBuilder: FormBuilder,
        private notificationService: NotificationService,
    ) {
        super(route, router, serverConfigService);
        this.detailForm = this.formBuilder.group({
            name: ['', Validators.required],
            conditions: this.formBuilder.array([]),
            actions: this.formBuilder.array([]),
        });
    }

    ngOnInit() {
        this.init();
        this.facets$ = this.dataService.facet
            .getFacets(9999999, 0)
            .mapSingle(data => data.facets.items)
            .pipe(shareReplay(1));

        this.promotion$ = this.entity$;
        this.dataService.promotion.getAdjustmentOperations().single$.subscribe(data => {
            this.allActions = data.adjustmentOperations.actions;
            this.allConditions = data.adjustmentOperations.conditions;
        });
    }

    ngOnDestroy() {
        this.destroy();
    }

    getAvailableConditions(): ConfigurableOperation[] {
        return this.allConditions.filter(o => !this.conditions.find(c => c.code === o.code));
    }

    getAvailableActions(): ConfigurableOperation[] {
        return this.allActions.filter(o => !this.actions.find(a => a.code === o.code));
    }

    saveButtonEnabled(): boolean {
        return (
            this.detailForm.dirty &&
            this.detailForm.valid &&
            this.conditions.length !== 0 &&
            this.actions.length !== 0
        );
    }

    addCondition(condition: ConfigurableOperation) {
        this.addOperation('conditions', condition);
        this.detailForm.markAsDirty();
    }

    addAction(action: ConfigurableOperation) {
        this.addOperation('actions', action);
        this.detailForm.markAsDirty();
    }

    removeCondition(condition: ConfigurableOperation) {
        this.removeOperation('conditions', condition);
        this.detailForm.markAsDirty();
    }

    removeAction(action: ConfigurableOperation) {
        this.removeOperation('actions', action);
        this.detailForm.markAsDirty();
    }

    formArrayOf(key: 'conditions' | 'actions'): FormArray {
        return this.detailForm.get(key) as FormArray;
    }

    create() {
        if (!this.detailForm.dirty) {
            return;
        }
        const formValue = this.detailForm.value;
        const input: CreatePromotionInput = {
            name: formValue.name,
            enabled: true,
            conditions: this.mapOperationsToInputs(this.conditions, formValue.conditions),
            actions: this.mapOperationsToInputs(this.actions, formValue.actions),
        };
        this.dataService.promotion.createPromotion(input).subscribe(
            data => {
                this.notificationService.success(_('common.notify-create-success'), { entity: 'Promotion' });
                this.detailForm.markAsPristine();
                this.changeDetector.markForCheck();
                this.router.navigate(['../', data.createPromotion.id], { relativeTo: this.route });
            },
            err => {
                this.notificationService.error(_('common.notify-create-error'), {
                    entity: 'Promotion',
                });
            },
        );
    }

    save() {
        if (!this.detailForm.dirty) {
            return;
        }
        const formValue = this.detailForm.value;
        this.promotion$
            .pipe(
                take(1),
                mergeMap(promotion => {
                    const input: UpdatePromotionInput = {
                        id: promotion.id,
                        name: formValue.name,
                        conditions: this.mapOperationsToInputs(this.conditions, formValue.conditions),
                        actions: this.mapOperationsToInputs(this.actions, formValue.actions),
                    };
                    return this.dataService.promotion.updatePromotion(input);
                }),
            )
            .subscribe(
                data => {
                    this.notificationService.success(_('common.notify-update-success'), {
                        entity: 'Promotion',
                    });
                    this.detailForm.markAsPristine();
                    this.changeDetector.markForCheck();
                },
                err => {
                    this.notificationService.error(_('common.notify-update-error'), {
                        entity: 'Promotion',
                    });
                },
            );
    }

    /**
     * Update the form values when the entity changes.
     */
    protected setFormValues(entity: Promotion.Fragment, languageCode: LanguageCode): void {
        this.detailForm.patchValue({ name: entity.name });
        entity.conditions.forEach(o => {
            this.addOperation('conditions', o);
        });
        entity.actions.forEach(o => this.addOperation('actions', o));
    }

    /**
     * Maps an array of conditions or actions to the input format expected by the GraphQL API.
     */
    private mapOperationsToInputs(
        operations: ConfigurableOperation[],
        formValueOperations: any,
    ): ConfigurableOperationInput[] {
        return operations.map((o, i) => {
            return {
                code: o.code,
                arguments: Object.values(formValueOperations[i].args).map((value, j) => ({
                    name: o.args[j].name,
                    value: value.toString(),
                    type: o.args[j].type,
                })),
            };
        });
    }

    /**
     * Adds a new condition or action to the promotion.
     */
    private addOperation(key: 'conditions' | 'actions', operation: ConfigurableOperation) {
        const operationsArray = this.formArrayOf(key);
        const collection = key === 'conditions' ? this.conditions : this.actions;
        const index = operationsArray.value.findIndex(o => o.code === operation.code);
        if (index === -1) {
            const argsHash = operation.args.reduce(
                (output, arg) => ({
                    ...output,
                    [arg.name]: arg.value,
                }),
                {},
            );
            operationsArray.push(
                this.formBuilder.control({
                    code: operation.code,
                    args: argsHash,
                }),
            );
            collection.push(operation);
        }
    }

    /**
     * Removes a condition or action from the promotion.
     */
    private removeOperation(key: 'conditions' | 'actions', operation: ConfigurableOperation) {
        const operationsArray = this.formArrayOf(key);
        const collection = key === 'conditions' ? this.conditions : this.actions;
        const index = operationsArray.value.findIndex(o => o.code === operation.code);
        if (index !== -1) {
            operationsArray.removeAt(index);
            collection.splice(index, 1);
        }
    }
}
