using System;
using System.Text;
using System.Linq;
using DevExpress.ExpressApp;
using System.ComponentModel;
using DevExpress.ExpressApp.DC;
using System.Collections.Generic;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Updating;
using DevExpress.ExpressApp.Model.Core;
using DevExpress.ExpressApp.Model.DomainLogics;
using DevExpress.ExpressApp.Model.NodeGenerators;
using DevExpress.ExpressApp.Xpo;
using DevExpress.ExpressApp.Security;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp.Security.Strategy;
using DevExpress.Persistent.BaseImpl.PermissionPolicy;

namespace Siegelportal.Module {
    // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppModuleBasetopic.aspx.
    public sealed partial class SiegelportalModule : ModuleBase {
        public SiegelportalModule() {
            InitializeComponent();
			BaseObject.OidInitializationMode = OidInitializationMode.AfterConstruction;
        }
        public override IEnumerable<ModuleUpdater> GetModuleUpdaters(IObjectSpace objectSpace, Version versionFromDB)
        {
            AddAdminDefault(objectSpace);
            ModuleUpdater updater = new DatabaseUpdate.Updater(objectSpace, versionFromDB);
            return new ModuleUpdater[] { updater };
        }
        public override void Setup(XafApplication application) {
            base.Setup(application);
            // Manage various aspects of the application UI and behavior at the module level.
        }
        public override void CustomizeTypesInfo(ITypesInfo typesInfo) {
            base.CustomizeTypesInfo(typesInfo);
            CalculatedPersistentAliasHelper.CustomizeTypesInfo(typesInfo);
        }

       

        public void AddAdminDefault(IObjectSpace objectspace)
        {
            PermissionPolicyRole administratorRole = objectspace.FindObject<PermissionPolicyRole>(new BinaryOperator("Name", SecurityStrategy.AdministratorRoleName));
            if (administratorRole == null)
            {
                administratorRole = objectspace.CreateObject<PermissionPolicyRole>();
                administratorRole.Name = SecurityStrategy.AdministratorRoleName;
                administratorRole.PermissionPolicy = SecurityPermissionPolicy.AllowAllByDefault;
                administratorRole.IsAdministrative = true;
                administratorRole.CanEditModel = true;
                administratorRole.Save();
                Role modelPermission = objectspace.CreateObject<Role>();
                modelPermission.Save();

                //administratorRole.AddObjectPermission()
                //administratorRole.AddPermission(new EditModelPermission(ModelAccessModifier.Allow));
                administratorRole.Save();

            }

            PermissionPolicyUser _user = objectspace.FindObject<PermissionPolicyUser>(new BinaryOperator("UserName", "Admin"));
            if (_user == null)
            {
                _user = objectspace.CreateObject<PermissionPolicyUser>();
                _user.UserName = "Admin";
                  
                _user.Roles.Add(administratorRole);
                _user.IsActive = true;
                _user.Save();
            }

            _user.Roles.Add(administratorRole);
            _user.Save();
        }

    }
}
