<?php

$text = $languageForm['changeEmailAddress'];

$error['changeEmailAddress']['form'] = [];

// TODO: Lock account after 3 incorrect email attempts

do {
    if (isset($_POST) && isset($_POST['oldEmailAddress']) && isset($_POST['newEmailAddress']) && isset($_POST['newEmailAddressRepeat'])) {
        if (empty($_POST['oldEmailAddress'])) {
            $error['changeEmailAddress']['form']['oldEmailAddress'][] = $languageValidation['oldEmailAddress']['name'] . $languageStd['empty'];
        }

        if (!filter_var($_POST['oldEmailAddress'], FILTER_VALIDATE_EMAIL)) {
            $error['changeEmailAddress']['form']['oldEmailAddress'][] = $languageValidation['oldEmailAddress']['validation'];
        }

        if (empty($_POST['newEmailAddress'])) {
            $error['changeEmailAddress']['form']['newEmailAddress'][] = $languageValidation['newEmailAddress']['name'] . $languageStd['empty'];
        }

        if (!filter_var($_POST['newEmailAddress'], FILTER_VALIDATE_EMAIL)) {
            $error['changeEmailAddress']['form']['newEmailAddress'][] = $languageValidation['newEmailAddress']['validation'];
        }

        if (empty($_POST['newEmailAddressRepeat'])) {
            $error['changeEmailAddress']['form']['newEmailAddressRepeat'][] = $languageValidation['newEmailAddressRepeat']['name'] . $languageStd['empty'];
        }

        if (!filter_var($_POST['newEmailAddressRepeat'], FILTER_VALIDATE_EMAIL)) {
            $error['changeEmailAddress']['form']['newEmailAddressRepeat'][] = $languageValidation['newEmailAddressRepeat']['validation'];
        }

        if (strtolower($_POST['newEmailAddress']) != strtolower($_POST['newEmailAddressRepeat'])) {
            $error['changeEmailAddress']['form']['newEmailAddressRepeat'] = [];
            $error['changeEmailAddress']['form']['didntMatch'][] = $languageError['changeEmailAddress']['didntMatch'];
        }

        if (strtolower($_POST['newEmailAddress']) == strtolower($_POST['oldEmailAddress'])) {
            $error['changeEmailAddress']['form']['newEmailAddress'] = [];
            $error['changeEmailAddress']['form']['newEmailAddressRepeat'] = [];
            $error['changeEmailAddress']['form']['oldNewSame'][] = $languageError['changeEmailAddress']['oldNewSame'];
        }

        if (count($error['changeEmailAddress']['form'])) {
            break;
        }

        $data = [
            'id' => $_SESSION['id'],
            'oldEmailAddress' => $_POST['oldEmailAddress'],
            'newEmailAddress' => $_POST['newEmailAddress']
        ];

        $json = "";

        if (api(API_REQ_CHANGE_EMAIL_ADDRESS, $data, $json)) {
            $success['changeEmailAddress'][] = $languageSuccess['changeEmailAddress']['changed'];

            break;
        }

        if (isset($json['code']) && $json['code'] == 2) {
            $error['changeEmailAddress']['form']['mailInUse'][] = $languageError['changeEmailAddress']['mailInUse'];
        } else {
            $error['changeEmailAddress']['form']['oldWrong'][] = $languageError['changeEmailAddress']['oldWrong'];
        }
    }
} while (0);
