using Bizgaze.CRM;
using Bizgaze.CRM.Visitors.Managers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unibase.Data;
using Unibase.Data.Expressions;
using Unibase.Data.Sql;
using Unibase.Dependency;
using Unibase.Platform.Apps.Managers;
using Unibase.Platform.Data;
using Unibase.Platform.Forms.Providers;
using Unibase.Platform.Forms.Requests;
using Unibase.Platform.Keys;

namespace Bizgaze.CRM.Visitors.ActionProviders
{
    public class Visitorphoneverifypreaction : BaseFormActionProvider, IFormActionProvider
    {
        public string ControllerClass
        {
            get
            {
                return "Bizgaze.CRM.Visitors.ActionProviders.Visitorphoneverifypreaction";

            }
        }
        public string FormActionName
        {
            get
            {
                return "VisitorPhonenumberVerify";
            }
        }
        public string Description
        {
            get
            {
                return "Visitor Phonenumber Verify";
            }
        }
        public UniqueKey UniqueId
        {
            get
            {
                return Bizgaze.CRM.Actions.VisitorPhonenumberVerify;
            }
        }
        public int ActionType
        {
            get
            {
                return (int)Unibase.Platform.Forms.Enums.FormActionProvider.PreAction;
            }
        }
        //public ActionResponse<ExpandoObject> Execute(ActionRequest request, object idValue, ExpandoObject obj, List<Unibase.Platform.Forms.Requests.DocParameter> docParametersList, ref long defaultPKValue)
        //{

        //    return null;
        //}

        // Verifying Phone Number Before Saving Visitor
        public async Task<ActionResponse<FormActionResponse>> ExecuteAsync(ActionRequest request, FormActionProviderObject providerObject)
        {
            var IdValue = providerObject.IdValue;
            var obj = providerObject.RequestObject;
            var defaultPKValue = providerObject.DefaultPKValue;
            var visitorManager = Unibase.Dependency.DependencyConfig.Resolve<IVisitorManager>();
            var dictObj = obj as IDictionary<string, dynamic>;            
            var vistortypeid = Convert.ToInt32(dictObj["visitortypeid"].ToString());
            if (vistortypeid != 2)
            {                
                if (dictObj.ContainsKey("isverified"))
                {
                    var isverified = dictObj["isverified"].ToString();
                    await Task.Delay(0);
                    if (isverified == "false")
                    {
                        return ActionResponse<FormActionResponse>.CreateErrorResponse("1", "Please Verify Mobile Number", null);
                    }
                }
            }
            var returnresponse = new FormActionResponse();
            returnresponse.Obj = dictObj as ExpandoObject;
            returnresponse.PkValue = defaultPKValue;
            return ActionResponse<FormActionResponse>.CreateSuccessResponse("success", returnresponse);
        }

        public List<UniqueKey> AppliedApps
        {
            get
            {
                return new List<UniqueKey>() {
                    Bizgaze.CRM.Actions.VisitorPhonenumberVerify,
            };
            }
        }

    }
}

