const removeValidation = getTextTranslate("sign-up", "remove-mask");

function validateFormPersonalData(dataDriver) {
    if (!ValidateForm.validateNameComplete(dataDriver.driver.name)) {
        SwalCustom.messageDialog(getTextTranslate("sign-up", "name-user-invalid"), getTextTranslate("sign-up", "title-required"), () => {
        }, "warning");
        return false;
    }
    if(!removeValidation && dataDriver.driver.document.replace(/\D/g, '').length <= 11) {
        if (!ValidateForm.validateCpf(dataDriver.driver.document.replace(/\D/g, ''))) {
            SwalCustom.messageDialog(getTextTranslate("sign-up", "document-user-invalid"), getTextTranslate("sign-up", "title-required"), () => {
            }, "warning");
            return false;
        }
    } else if(!removeValidation && dataDriver.driver.document.replace(/\D/g, '').length < 14){
        SwalCustom.messageDialog(getTextTranslate("sign-up", "document-user-invalid"), getTextTranslate("sign-up", "title-required"), () => {
        }, "warning");
        return false;
    }

    if (!ValidateForm.validateEmail(dataDriver.driver.email)) {
        SwalCustom.messageDialog(getTextTranslate("sign-up", "email-user-invalid"), getTextTranslate("sign-up", "title-required"), () => {
        }, "warning");
        return false;
    }
    if (!removeValidation && !ValidateForm.validatePhone(dataDriver.driver.phone)) {
        SwalCustom.messageDialog(getTextTranslate("sign-up", "phone-user-invalid"), getTextTranslate("sign-up", "title-required"), () => {
        }, "warning");
        return false;
    }

    if (dataDriver.driver.gender.length === 0) {
        SwalCustom.messageDialog(getTextTranslate("sign-up", "gender-user-invalid"), getTextTranslate("sign-up", "title-required"), () => {
        }, "warning");
        return false;
    }

    return true;
}

function getVerifyCode(dataDriver) {
    const driverController = new DriverController();

    navigator.geolocation.getCurrentPosition(
        (position) => {
            latitude = position.coords.latitude;
            longitude = position.coords.longitude;
        },
        (error) => {    
            console.log(error);
        },
    );

    const payload = {
        number: dataDriver.driver.phone,
        latitude: latitude,
        longitude: longitude
    };

    driverController.sendConfirmationCode(payload).then(result => {
        document.getElementById("verifyCode").value = result.data.sms;
    });
}

function clearCode() {
    const elementProperty = new ElementProperty();

    elementProperty.getElement(".code-phone", element => {
        element.value = "";
    });
}

function verifyCode(validateForm) {
    if (validateForm.verifyCode.toString() === validateForm.confirmationCode ||
        Session.get("codeSupport") === validateForm.confirmationCode) {
        return true;
    }

    SwalCustom.messageDialog(getTextTranslate("sign-up", "confirmation-code-invalid"), getTextTranslate("sign-up", "title-required"), () => {
    }, "info");
    return false;
}


function verifyPassword(dataDriver) {

    if (document.getElementById("repassowrd").value !== dataDriver.driver.password || dataDriver.driver.password.length === 0) {
        SwalCustom.messageDialog(getTextTranslate("sign-up", "passwords-do-not-match"), getTextTranslate("sign-up", "title-required"), () => {
        }, "warning");
        return false;
    }
    return true;
}

function validateMilitaryUser(dataDriver) {
    if (dataDriver.driver.militaryUser.militaryForceId.length === 0) {
        swal('Atenção', "Informe o tipo de força militar", 'warning');
        return false;
    }

    if (dataDriver.driver.militaryUser.militaryGraduationId.length === 0) {
        swal('Atenção', "Informe de patente", 'warning');
        return false;
    }

    if (dataDriver.driver.militaryUser.militaryStatusId.length === 0) {
        swal('Atenção', "Informe seu status atual", 'warning');
        return false;
    }

    if (dataDriver.driver.militaryUser.om.length === 0) {
        swal('Atenção', "Informe o número da OM", 'warning');
        return false;
    }

    return true;
}


function verifyRGMilitary(dataDriver) {
    if (dataDriver.image.RG_FRONT === null) {
        swal('Atenção', "Retire uma foto frontal do RG", 'warning');
        return false;
    }

    if (dataDriver.image.RG_BACK === null) {
        swal('Atenção', "Retire uma foto do verso do RG", 'warning');
        return false;
    }

    return true;
}

function verifyCompany(dataDriver) {
    if (dataDriver.driver.cityId === null) {
        SwalCustom.messageDialog(getTextTranslate("sign-up", "city-user-required"), getTextTranslate("sign-up", "title-required"), () => {
        }, "warning");
        return false;
    }

    return true;
}

function verifyDocumentCNH(dataDriver) {
    if (dataDriver.driver.numberCNH.length === 0) {
        SwalCustom.messageDialog("Por favor digite o número da CNH", "Atenção", () => {
        }, "warning");
        return false;
    }

    if (dataDriver.image.CNH === null) {
        SwalCustom.messageDialog("Retire uma foto  do seu CNH", "Atenção", () => {
        }, "warning");
        return false;
    }

    return true;
}

function verifyDocumentCRLV(dataDriver) {

    if (dataDriver.car.licensePlate.length === 0) {
        SwalCustom.messageDialog(getTextTranslate("sign-up","license-place-required"), getTextTranslate("sign-up", "title-required"), () => {
        }, "warning");
        return false;
    }

    let regex = /^[a-zA-Z]{3}[0-9]{4}\b/;
    let newRegex = /^[a-zA-Z]{3}[0-9]{1}[a-zA-Z]{1}[0-9]{2}\b/;

    if (!removeValidation && !regex.test(dataDriver.car.licensePlate.replace("-", "")) && !newRegex.test(dataDriver.car.licensePlate.replace("-", ""))) {
        SwalCustom.messageDialog(getTextTranslate("sign-up","license-place-invalid"), getTextTranslate("sign-up", "title-required"), () => {
        }, "warning");
        return false;
    }

    if (dataDriver.car.renavam.length === 0) {
        SwalCustom.messageDialog(getTextTranslate("sign-up","renavam-required"),  getTextTranslate("sign-up", "title-required"), () => {
        }, "warning");
        return false;
    }

    if (dataDriver.car.markCar.length === 0) {
        SwalCustom.messageDialog(getTextTranslate("sign-up","mark-car-required"), getTextTranslate("sign-up", "title-required"), () => {
        }, "warning");
        return false;
    }

    if (dataDriver.car.modelCar.length === 0) {
        SwalCustom.messageDialog(getTextTranslate("sign-up","model-car-required"), getTextTranslate("sign-up", "title-required"), () => {
        }, "warning");
        return false;
    }

    if (dataDriver.car.colorCar.length === 0) {
        SwalCustom.messageDialog(getTextTranslate("sign-up","color-car-required"), getTextTranslate("sign-up", "title-required"), () => {
        }, "warning");
        return false;
    }

    if (dataDriver.car.numberSeatingCar.length === 0) {
        SwalCustom.messageDialog(getTextTranslate("sign-up","number-seating-car-required"), getTextTranslate("sign-up", "title-required"), () => {
        }, "warning");
        return false;
    }
    if (dataDriver.car.year.length === 0) {
        SwalCustom.messageDialog(getTextTranslate("sign-up","year-vehicle-required"), getTextTranslate("sign-up", "title-required"), () => {
        }, "warning");
        return false;
    }

    /*if(dataDriver.image.CRLV === null){
        SwalCustom.messageDialog("Retire uma foto do seu CRLV", "Atenção", () => {
        }, "warning");
        return false;
    }*/

    return true;
}

function verifyDocumentProofOfResidence(dataDriver) {
    if (dataDriver.image.PROOF_OF_RESIDENCE === null) {
        SwalCustom.messageDialog("Retire uma foto do seu COMPROVANTE DE RESIDÊNCIA", "Atenção", () => {
        }, "warning");
        return false;
    }

    return true;
}

function verifyDocumentALVARA(dataDriver) {
    if (dataDriver.driver.numberAlvara === null) {
        SwalCustom.messageDialog("Por favor digite o número do ALVARÁ", "Atenção", () => {
        }, "warning");
        return false;
    }

    if (dataDriver.image.ALVARA === null) {
        SwalCustom.messageDialog("Retire uma foto do seu ALVARA", "Atenção", () => {
        }, "warning");
        return false;
    }

    return true;
}


function verifyDocumentCriminalRecord(dataDriver) {
    if (dataDriver.driver.numberCriminalRecord.length === 0) {
        SwalCustom.messageDialog("Por favor, digite o número do atestado de antecedentes criminais", "Atenção", () => {
        }, "warning");
        return false;
    }

    if (dataDriver.image.CRIMINAL_RECORD == null) {
        SwalCustom.messageDialog("Por favor, informer a certidão de antecedentes criminais", "Atenção", () => {
        }, "warning");
        return false;
    }

    return true;
}


function verifyDocumentProfile(dataDriver) {
    if (dataDriver.image.PROFILE === null) {
        SwalCustom.messageDialog(getTextTranslate("sign-up","photo-profile-required"), getTextTranslate("sign-up", "title-required"), () => {
        }, "warning");
        return false;
    }

    return true;
}

function verifyDocument(dataDriver) {
    if (dataDriver.image.CNH === null) {
        SwalCustom.messageDialog("Retire uma foto  do seu CNH", "Atenção", () => {
        }, "warning");
        return false;
    }

    if (dataDriver.image.CRLV === null) {
        SwalCustom.messageDialog("Retire uma foto do seu CRLV", "Atenção", () => {
        }, "warning");
        return false;
    }

    if (dataDriver.image.ALVARA === null) {
        SwalCustom.messageDialog("Retire uma foto do seu ALVARA", "Atenção", () => {
        }, "warning");
        return false;
    }

    if (dataDriver.image.PROFILE === null) {
        SwalCustom.messageDialog("Retire uma foto do seu rosto", "Atenção", () => {
        }, "warning");
        return false;
    }
    return true;
}
