using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using rockfishCore.Models;
using System.IO;

namespace rockfishCore.Util
{

    public static class CustomerUtils
    {

        /// <summary>
        /// Adds email if not already present, handles trimming and check for duplicates etc
        /// **DOES NOT CALL SAVE ON CONTEXT, CALLER IS RESPONSIBLE**
        /// </summary>
        /// <param name="cust"></param>
        /// <param name="newEmail"></param>  
        public static void AddAdminEmailIfNotPresent(Customer cust, string newEmail)
        {
            AddEmailIfNotPresent(cust, newEmail, true);
        }

        /// <summary>
        /// Adds email if not already present, handles trimming and check for duplicates etc
        /// **DOES NOT CALL SAVE ON CONTEXT, CALLER IS RESPONSIBLE**
        /// </summary>
        /// <param name="cust"></param>
        /// <param name="newEmail"></param>  
        public static void AddSupportEmailIfNotPresent(Customer cust, string newEmail)
        {
            AddEmailIfNotPresent(cust, newEmail, false);
        }

        /// <summary>
        /// Adds email if not already present, handles trimming and check for duplicates etc
        /// **DOES NOT CALL SAVE ON CONTEXT, CALLER IS RESPONSIBLE**
        /// </summary>
        /// <param name="cust"></param>
        /// <param name="newEmail"></param>
        /// <param name="isAdmin"></param>
        private static void AddEmailIfNotPresent(Customer cust, string newEmail, bool isAdmin)
        {
            newEmail = newEmail.Trim();
            string newEmailAsLower = newEmail.ToLowerInvariant();
            string compareTo = cust.AdminEmail;
            if (false == isAdmin)
                compareTo = cust.SupportEmail;

            bool noPriorAddress = false;

            if (!string.IsNullOrWhiteSpace(compareTo))
                compareTo = compareTo.ToLowerInvariant();
            else
                noPriorAddress = true;

            //See if email is already present
            if (false == noPriorAddress && compareTo.Contains(newEmailAsLower))
            {
                return;//skip this one, it's already there
            }

            //It's not in the field already so add it
            if (noPriorAddress)
            {
                if (isAdmin)
                    cust.AdminEmail = newEmail;
                else
                    cust.SupportEmail = newEmail;
            }
            else
            {
                if (isAdmin)
                    cust.AdminEmail = cust.AdminEmail + ", " + newEmail;
                else
                    cust.SupportEmail = cust.SupportEmail + ", " + newEmail;
            }
        }


    }//eoc

}//eons