using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ServiceModel;
using System.ServiceProcess;
using System.Configuration;
using System.Configuration.Install;
using AutoFileCopyNet;
using Cjwdev;
using Cjwdev.WindowsApi;
using System.Runtime.InteropServices;
namespace Microsoft.ServiceModel.Samples
{
    // Define a service contract.
    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
    public interface IBarCodePrint
    {
        [OperationContract]
        bool Print(string[] barCode, string barCodeXmlName);
    }

    // Implement the IBarCodePrint service contract in a service class.
    public class BarCodePrintService : IBarCodePrint
    {
        // Implement the IBarCodePrint methods.
        public bool Print(string[] barCode, string barCodeType)
        {
            bool b = false;

            try
            {

                for (int i = 0; i < barCode.Length; i++)
                {
                    QM.Assist.LabelInfo ll = new QM.Assist.LabelInfo();
                    ll.BarCode = barCode[i].Trim();
                    QM.Assist.PrintUtil.LabelList2.Add(ll);
                }



                QM.Assist.PrintUtil pu = new QM.Assist.PrintUtil();
                pu.PrintLabel2(System.Configuration.ConfigurationManager.AppSettings["proPath"].ToString(),
                    System.Configuration.ConfigurationManager.AppSettings["BPtemPath"].ToString(),
                    System.Configuration.ConfigurationManager.AppSettings["BPdataPath"].ToString(),
                    System.Configuration.ConfigurationManager.AppSettings[barCodeType + "char"].ToString());
                b = true;
            }
            catch (Exception ex)
            {
                b = false;
                LogFileAccess.WriteLogFile(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + "printError:" + ex.Message);
            }
            return b;
        }

    }

    public class BarCodePrintWindowsService : ServiceBase
    {
        public ServiceHost serviceHost = null;
        public BarCodePrintWindowsService()
        {
            // Name the Windows Service
            ServiceName = "WCFWindowsServiceSample";
        }

        public static void Main()
        {
            ServiceBase.Run(new BarCodePrintWindowsService());
        }

        // Start the Windows service.
        protected override void OnStart(string[] args)
        {
            try
            {
                LogFileAccess.WriteLogFile(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + "服务开始启动");
                if (serviceHost != null)
                {
                    serviceHost.Close();
                }

                // Create a ServiceHost for the BarCodePrintService type and 
                // provide the base address.
                serviceHost = new ServiceHost(typeof(BarCodePrintService));

                // Open the ServiceHostBase to create listeners and start 
                // listening for messages.
                LogFileAccess.WriteLogFile(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + "服务启动");
                serviceHost.Open();
                LogFileAccess.WriteLogFile(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + "服务启动成功");
            }
            catch (Exception ex)
            {
                LogFileAccess.WriteLogFile(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + "服务启动错误:" + ex.Message + ex.Source);
                throw;
            }

        }

        protected override void OnStop()
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
                serviceHost = null;
            }
        }
    }

    // Provide the ProjectInstaller class which allows 
    // the service to be installed by the Installutil.exe tool
    [RunInstaller(true)]
    public class ProjectInstaller : Installer
    {
        private ServiceProcessInstaller process;
        private ServiceInstaller service;

        public ProjectInstaller()
        {
            process = new ServiceProcessInstaller();
            process.Account = ServiceAccount.LocalSystem;
            service = new ServiceInstaller();
            service.ServiceName = "WCFWindowsServiceSample";
            Installers.Add(process);
            Installers.Add(service);
        }


    }
}