using Bentley.DgnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using Bentley.MstnPlatformNET;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LinkageCRUDDemo
{
    class PipeInformation
    {
        public double Diameter;
        public double Temperature;
        public string Material;
        public PipeInformation()
        {
            Diameter = double.MinValue;
            Temperature = double.MinValue;
            Material = null;
        }

        public PipeInformation(double diam,double temp,string mate)
        {
            Diameter = diam;
            Temperature = temp;
            Material = mate;
        }
    }

    class KeyinCommands
    {
        public static ushort LinkageId = 123;
        public static Element Ele1
        {
            get
            {
                ElementAgenda eleAgen = new ElementAgenda();
                SelectionSetManager.BuildAgenda(ref eleAgen);
                if (eleAgen.GetCount() < 1)
                    return null;
                Element ele1 = eleAgen.GetEntry(0);
                return ele1;
            }
        }
        public static void CreateLinkage(PipeInformation pipeInfo)
        {
            Element oldEle = Element.GetFromElementRef(Ele1.GetNativeElementRef());
            WriteDataBlock wrDataBlk = new WriteDataBlock();
            wrDataBlk.WriteDouble(pipeInfo.Diameter);
            wrDataBlk.WriteDouble(pipeInfo.Temperature);
            wrDataBlk.WriteString(pipeInfo.Material);
            oldEle.AppendLinkage(LinkageId, wrDataBlk);
            oldEle.ReplaceInModel(oldEle);
        }
        public static PipeInformation ReadLinkage()
        {
            PipeInformation rtnPipeInfo = null;
            ReadDataBlock reDataBlk = Ele1.GetLinkage(LinkageId);
            if (null != reDataBlk)
            {
                rtnPipeInfo = new PipeInformation();
                rtnPipeInfo.Diameter = reDataBlk.ReadDouble();
                rtnPipeInfo.Temperature = reDataBlk.ReadDouble();
                rtnPipeInfo.Material = reDataBlk.ReadString();
            }
            return rtnPipeInfo;
        } 

        public static void DeleteLinkage()
        {
            Element oldEle = Element.GetFromElementRef(Ele1.GetNativeElementRef());
            oldEle.DeleteLinkage(LinkageId);
            oldEle.ReplaceInModel(oldEle);
        }

        public static void UpdateLinkage(PipeInformation pipeInfo)
        {
            DeleteLinkage();
            CreateLinkage(pipeInfo);
        }

        public static void TestFun(string unparsed)
        {
            unparsed = unparsed.Trim().ToLower();
            if (unparsed == "create")
            {
                PipeInformation pipeInfo = new PipeInformation(0.5, 80, "Q235A");
                CreateLinkage(pipeInfo);
            }
            else if (unparsed == "read")
            {
                PipeInformation pipeInfo = ReadLinkage();
                if(null!=pipeInfo)
                {
                    string msg = string.Format("Diameter={0},Temperature={1},Material={2}", pipeInfo.Diameter, pipeInfo.Temperature, pipeInfo.Material);
                    MessageCenter.Instance.ShowInfoMessage(msg, "", false);
                }
            }
            else if(unparsed == "update")
            {
                PipeInformation pipeInfo = new PipeInformation(1, 90, "Q235B");
                UpdateLinkage(pipeInfo);
            }
            else if (unparsed == "delete")
            {
                DeleteLinkage();
            }
        }
    }
}
