using System.Collections.Generic;
using Bentley.DgnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using Bentley.DgnPlatformNET.DgnEC;
using Bentley.MstnPlatformNET;
using Bentley.ECObjects;
using Bentley.ECObjects.Schema;
using Bentley.ECObjects.Instance;
using System.Windows.Forms;
using System;
using Bentley.ECObjects.UI;
using System.ComponentModel;
using System.Collections;

namespace DisableECName
{
    internal class KeyinCommands
    {
        static string NameECSchema = "MyECSchema";
        static string NameECClass = "MyECClass";
        static string NameProp = "Prop1";
        public static ECSchema CreateECSchema()
        {
            ECSchema ecSchema = new ECSchema(NameECSchema, 1, 0, "Sample");
            IECClass ecClass = ecSchema.AddClass(NameECClass);
            IECProperty ecprop = ecClass.AddProperty(NameProp, ECObjects.StringType);
            int verMaj = 1, verMin = 0;
            FindInstancesScope scopeTemp = FindInstancesScope.CreateScope(Session.Instance.GetActiveDgnFile(), new FindInstancesScopeOption());
            IECSchema schemaCustomAtt = DgnECManager.Manager.LocateSchemaInScope(scopeTemp, "DgnCustomAttributes", verMaj, verMin,
                      SchemaMatchType.LatestCompatible);
            if (schemaCustomAtt != null)
            {
                IECClass ecClassCate = schemaCustomAtt.GetClass("ItemIdentifiesHost");
                IECInstance ecInsCate = ecClassCate.CreateInstance();
                ecInsCate.SetBoolean("ShouldIdentifyHost", false);
                ecClass.SetCustomAttribute(ecInsCate);
            }
            return ecSchema;
        }

        public static bool CheckECSchema()
        {
            DgnECManager manager = DgnECManager.Manager;
            IEnumerable<string> schemaNames = manager.DiscoverSchemas(Session.Instance.GetActiveDgnFile(), ReferencedModelScopeOption.All, true);
            foreach (string schemaName in schemaNames)
            {
                if (schemaName.Split('.')[0] == NameECSchema)
                {
                    return true;
                }
            }
            ECSchema ecSchema = CreateECSchema();
            if (null == ecSchema)
                return false;
            SchemaImportStatus schemaImportStatus = manager.ImportSchema(ecSchema, Session.Instance.GetActiveDgnFile(), new ImportSchemaOptions());
            if (schemaImportStatus != SchemaImportStatus.Success)
            {
                return false;
            }
            return true;
        }

        public static IECSchema GetECSchema()
        {
            FindInstancesScope scopeTemp = FindInstancesScope.CreateScope(Session.Instance.GetActiveDgnFile(), new FindInstancesScopeOption());
            int verMaj = 1, verMin = 0;
            IECSchema ecschema = DgnECManager.Manager.LocateSchemaInScope(scopeTemp, NameECSchema, verMaj, verMin, SchemaMatchType.Latest);
            return ecschema;
        }
         

        public static void TestFun(string unparsed)
        {
            ElementAgenda eleAgen = new ElementAgenda();
            SelectionSetManager.BuildAgenda(ref eleAgen);
            if (eleAgen.GetCount() < 1)
                return;
            Element ele = eleAgen.GetEntry(0);
            IECSchema ecSchema = GetECSchema();
            IECClass ecClass = ecSchema.GetClass(NameECClass);
            DgnECInstanceEnabler instanceEnabler = DgnECManager.Manager.ObtainInstanceEnabler(Session.Instance.GetActiveDgnFile(), ecClass);
            StandaloneECDInstance instance = instanceEnabler.SharedWipInstance;
            instance.SetString(NameProp, "MyVal");
            instanceEnabler.CreateInstanceOnElement(ele, instance, false);
        }
    }


}
