using Bizgaze.CRM;
using Bizgaze.CRM.Visitors.Managers;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Text.Json;
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.Keys;

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

            }
        }
        public string FormActionName
        {
            get
            {
                return "UpdateVisitorBeenHere";
            }
        }
        public string Description
        {
            get
            {
                return "Update Visitor Been Here PreAction";
            }
        }
        public UniqueKey UniqueId
        {
            get
            {
                return Bizgaze.CRM.Actions.UpdateVisitorBeenHere;
            }
        }
        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;

        //}
        public async Task<ActionResponse<FormActionResponse>> ExecuteAsync(ActionRequest request, FormActionProviderObject providerObject)
        {
            var IdValue = providerObject.IdValue;
            var srequest = SelectRequest.FromRequest(request);
            var obj = providerObject.RequestObject;
            var docParametersList = providerObject.DocParametersList;
            var defaultPKValue = providerObject.DefaultPKValue;
            var stageManager = Unibase.Dependency.DependencyConfig.Resolve<IStageManager>();
            var dictObj = obj as IDictionary<string, dynamic>;
            dynamic Visitorid = Convert.ChangeType(dictObj["mobilenumber"].ToString(), Type.GetType("System.Int64"));  
           var updateValues = new Dictionary<string, object>();
           updateValues.Add("agreement", true);
           var updateQry = new Update("bizgazecrm_visitors").Values(updateValues).Where(Exp.Eq("visitorid", Visitorid));
            var ds = await _repository.ExecuteAsync(request, updateQry);
            //visitorObj = JsonSerializer.Deserialize<List<Infos.Visitor>>(data);
            //var visitorId= Convert.ToInt64(visitorObj[0].MobileNumber);
            //var VisitorManager = DependencyConfig.Resolve<IVisitorManager>();
            //var visitorsInfo = await VisitorManager.GetVisitorPrAsyncintId(Srequest, visitorId);
            var returnresponse = new FormActionResponse();
            returnresponse.Obj = null;
            returnresponse.PkValue = defaultPKValue;
            return ActionResponse<FormActionResponse>.CreateSuccessResponse("success", returnresponse);
        }

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

    }
}
