using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.NotificationServices;

namespace CreateSubscribersAndSubscriptions
{
  class Program
  {
    static void Main(string[] args)
    {
      CreateSubscribers();
      CreateSubscriptions();
    }

    static void CreateSubscribers()
    {
      NSInstance inst = new NSInstance();
      inst.Initialize("Stock");

      Subscriber s = new Subscriber();
      s.Initialize(inst);

      s.SubscriberId = "bobb";
      s.Add();

      s.SubscriberId = "mary";
      s.Add();

      s.SubscriberId = "dan";
      s.Add();

      SubscriberDevice dev = new SubscriberDevice();
      dev.Initialize(inst);

      dev.DeviceTypeName = "Email";
      dev.DeviceName = "myEmailDevice";
      dev.DeliveryChannelName = "EmailChannel";

      dev.SubscriberId = "bobb";
      dev.DeviceAddress = "bobb@SQLskills.com";
      dev.Add();

      dev.SubscriberId = "mary";
      dev.DeviceAddress = "mary@comcast.com";
      dev.Add();

      dev.SubscriberId = "dan";
      dev.DeviceAddress = "dan@pluralsight.com";
      dev.Add();
    }

    static void CreateSubscriptions()
    {
      NSInstance inst = new NSInstance();
      inst.Initialize("Stock");

      NSApplication app = new NSApplication();
      app.Initialize(inst, "StockPrice");

      Subscription sub = new Subscription();
      sub.Initialize(app, "StockPriceSubscriptions");

      sub.SetFieldValue("DeviceName", "myEmailDevice");
      sub.SetFieldValue("SubLocale", "en");

      sub.SubscriberId = "bobb";
      sub.SetFieldValue("RicCode", "IBM");
      sub.SetFieldValue("ExchangeCode", "NYSE");
      sub.SetFieldValue("TriggerVal", 119);
      sub.Add();

      sub.SubscriberId = "mary";
      sub.SetFieldValue("RicCode", "SUNW");
      sub.SetFieldValue("ExchangeCode", "NASDAQ");
      sub.SetFieldValue("TriggerVal", 7);
      sub.Add();

      sub.SubscriberId = "dan";
      sub.SetFieldValue("SubLocale", "sv");
      sub.SetFieldValue("RicCode", "MSFT");
      sub.SetFieldValue("ExchangeCode", "NYSE");
      sub.SetFieldValue("TriggerVal", 52);
      sub.Add();

    }
  }
}
