import { Injectable } from '@angular/core';
import { FormArray, FormControl, FormGroup } from '@angular/forms';
import { Observable, Subject } from 'rxjs';

@Injectable()
export class HelperService {

    private _hideTaskBar$ = new Subject<boolean>();

    hideTaskBar$(): Observable<boolean> {
        return this._hideTaskBar$.asObservable();
    }

    set hideTaskBar(value) {
        this._hideTaskBar$.next(value);
    }

    validateAllFormFields(form?: any): void {
        const formObj = typeof form.form ? form.form : form;
        Object
            .keys(form.controls)
            .forEach(field => {
                const control = formObj.get(field);
                if (control instanceof FormControl) {
                    control.markAsDirty({onlySelf: true});
                    control.markAsTouched({onlySelf: true});
                } else if (control instanceof FormGroup) {
                    this.validateAllFormFields(control);
                } else if (control instanceof FormArray) {
                    control.controls.forEach(ctrl => {
                        if (ctrl instanceof FormControl) {
                            ctrl.markAsDirty({onlySelf: true});
                            ctrl.markAsTouched({onlySelf: true});
                        } else if (ctrl instanceof FormGroup) {
                            this.validateAllFormFields(ctrl);
                        }
                    });
                }
            });
    }

    circleGradient(value = 0, completeColor= '#f0f', incompleteColor = '#EAECEE'): { backgroundImage: string, backgroundColor: string } {
        const percentage = value * 3.6;
        let grad;
        if (percentage <= 180) {
            grad = `linear-gradient(${90 + percentage}deg, transparent 50%, ${incompleteColor} 50%),linear-gradient(90deg, ${incompleteColor} 50%, transparent 50%)`;
        } else {
            grad = `linear-gradient(${percentage - 90}deg, transparent 50%, ${completeColor} 50%),linear-gradient(90deg, ${incompleteColor} 50%, transparent 50%)`;
        }
        return {backgroundImage: grad, backgroundColor: incompleteColor};
    }

}
