﻿<?xml version="1.0" encoding="utf-8"?>
<AlvaoApplication xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ModelVersion="1">
  <Applications>
    <Application id="31">
      <Name>Check Phone Number Format</Name>
      <Description>Check the format of the new Phone Number property value.</Description>
      <Scripts>
        <Script id="59">
          <Name>CheckPhoneNumberFormat</Name>
          <Code>
using System;
using System.Data;
using Microsoft.Data.SqlClient;
using Alvao.Apps.API;
using Alvao.API.AM;
using Alvao.API.Common.Model.Database;
using Dapper;

class ObjectPropertyAutoAction : IObjectPropertyAutoAction
{
  public ObjectPropertyModifyResult OnObjectPropertyModifying(ObjectPropertyEventArgs e)
  {
    if (Settings.propName != ObjectProperty.GetDefinition(e.PropertyKindId)?.txtName)
      return new ObjectPropertyModifyResult(true, (string)null);

    // Value was empty.
    if (e.NewValue is null)
      return new ObjectPropertyModifyResult(true, (string)null);

    // Check formatting.
    var matches = Settings.rx.Matches(e.NewValue.ToString());

    if (matches.Count == 0)
      return new ObjectPropertyModifyResult(false, Settings.errorMessage);

    // The number was entered in a valid format.
      return new ObjectPropertyModifyResult(true, (string)null);
  }

  public void OnObjectPropertyModified(ObjectPropertyEventArgs e)
  {
    throw new NotImplementedException();
  }
}
          </Code>
          <IsLibCode>false</IsLibCode>
        </Script>
        <Script id="61">
          <Name>Settings</Name>
          <Code>
            using System.Text.RegularExpressions;

            public class Settings
            {
            /*
            * The action to check the format of the new Phone Number property value.
            */

            public static readonly string propName = "Phone number"; // Specify the property name you want to check.
            public static readonly Regex rx = new Regex(@"^\+?(?:[\s\-\.]|\(\d+\)|\d+)+$", RegexOptions.Compiled | RegexOptions.IgnoreCase); // Phone number format in regex.
            public static readonly string errorMessage = "The value you entered is not a phone number. Enter the phone number in the correct format, etc. +(039) 123 456 789."; // Error message if the new value is not in correct format.

            // Do not change this value.
            public static bool isPhoneNumber = false;
            }
          </Code>
          <IsLibCode>true</IsLibCode>
        </Script>
      </Scripts>
    </Application>
  </Applications>
</AlvaoApplication>