import { Injectable } from '@angular/core';
import { AbstractControl, FormControl, ValidatorFn, Validators } from '@angular/forms';

@Injectable({
    providedIn: 'root',
})

export class ValidationService {
    regExps = {
        password: /^[0-9]{6}$/,
        passwordV2: /(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,<>\/?~])(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{8,}/,
        mobCode: /^[0-9]{2,4}$/,
        mobNum: /^[0-9]*$/,
        numbers: /[0-9]/,
        decimal: /^(0|[1-9.]\d*)(\.\d+)?$/,
        alpha_numeric: /^[a-zA-Z ]*$/,
        lower_case: /[a-z]/,
        Capital_case: /[A-Z]/,
        special_characters: /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/,
        lat_long: /^-?(?=.*\d\.\d)(?!.*\.\d*\.)\d+\.?\d*$/
    };
    get email() {
        return new FormControl('', [Validators.required, Validators.email]);
    }
    get email_non_required() {
        return new FormControl('', [Validators.email]);
    }
    get passwordV2() {
        return new FormControl('', [
            Validators.required,
            Validators.pattern(this.regExps['passwordV2']),
        ]);
    }
    get passwordV2_non_required() {
        return new FormControl('', [
            Validators.required,
            Validators.pattern(this.regExps['passwordV2']),
        ]);
    }
    get password() {
        return new FormControl('', [
            Validators.required,
            Validators.pattern(this.regExps['password']),
        ]);
    }
    get password_non_required() {
        return new FormControl('', [
            Validators.pattern(this.regExps['password']),
        ]);
    }
    get number() {
        return new FormControl('', [
            Validators.required,
            Validators.pattern(this.regExps['mobNum']),
        ]);
    }
    get mobNo() {
        return new FormControl('', [
            Validators.pattern(this.regExps['mobNum']),
            Validators.required,
        ]);
    }
    get alpha_numeric() {
        return new FormControl('', [
            Validators.pattern(this.regExps['alpha_numeric'])
        ]);
    }
    get alphaNumericWithReq() {
        return new FormControl('', [
            Validators.pattern(this.regExps['alpha_numeric']),
            Validators.required,
        ]);
    }
    get required() {
        return new FormControl(null, [Validators.required]);
    }

    get decimal() {
        return new FormControl('', [
            Validators.pattern(this.regExps['decimal']),
            Validators.required,
        ]);
    }
    get numreq() {
        return new FormControl('', [
            Validators.pattern(this.regExps['mobNum']),
            Validators.required,
        ]);
    }
    get lat_long() {
        return new FormControl('', [
            Validators.pattern(this.regExps['lat_long']),
            Validators.required,
        ]);
    }



    onBlur(Control, validationType) {
        return Control.hasError(validationType) && Control.touched;
    }


    getFormArrControls(formArrayName, index, controlName) {
        return formArrayName.controls[index].controls[controlName];
    }

    showValidationsMsg(form) {
        form.markAllAsTouched();
    }

    prioirtyValidation(alldata) {
        let message = null
        let pri_arr = alldata.filter((el, ind) => {
            return alldata.findIndex(e => e.priority == el.priority) != ind;
        });
        let negative_pr = alldata.filter(e => e.priority < 0);
        let max_pr = alldata.filter(e => e.priority > 100);
        let zero_pr = alldata.filter(e => e.priority == 0)
        if (negative_pr.length > 0) {
            message = "Priority can't be negative"
        } else if (pri_arr.length > 0) {
            message = "Priority can't be same"
        } else if (max_pr.length > 0) {
            message = "Priority can't be above 100"
        } else if (zero_pr.length > 0) {
            message = "Priority can't be 0"
        }
        return message
    }


    scrollTo(el: Element): void {
        if (el) {
            el.scrollIntoView({ behavior: 'smooth', block: 'center' });
        }
    }
    scrollToError(): void {
        const errorInput = document.querySelector('.ErrorWrapper');
        const errorspan = document.querySelector('.errorspan');
        this.scrollTo(errorInput ? errorInput : errorspan);
    }
    dateRangeValidation(): ValidatorFn {
        return (control: AbstractControl): { [key: string]: boolean } | null => {
            if (control.value) {
                if (control.value.length <= 1 || control.value[1] == null) {
                    return { 'todate': true };
                }
            }
            return null;
        };
    }



}