using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace ButcherFactory.BO.Utils
{
  public static class AppContext
  {
    public static bool RpcFacadeInited { get; set; }
    static ServerUrlConfig _connectInfo = ServerUrlConfig.Init();
    public static ServerUrlConfig ConnectInfo { get { return _connectInfo; } }

    static Worker _worker = null;
    public static Worker Worker
    {
      get
      {
        if (_worker == null)
          _worker = LoginUtil.InitUserFromLocal();
        return _worker;
      }
    }
  }

  public class ServerUrlConfig
  {
    public static ServerUrlConfig Init()
    {
      return XmlUtil.DeserializeFromFile<ServerUrlConfig>();
    }

    public int ServerMode { get; set; }

    public string MESUrl { get; set; }

    public string B3Url { get; set; }

    [XmlIgnore]
    public string MainUrl { get { return ServerMode == 0 ? MESUrl : B3Url; } }

    [XmlIgnore]
    public string SecondUrl { get { return ServerMode == 0 ? B3Url : MESUrl; } }

    public string ClientCode { get; set; }

    public string SqlConnection { get; set; }

    private string _traceBackUrl = "default";
    public string TraceBackUrl
    {
      get { return _traceBackUrl; }
      set { _traceBackUrl = value; }
    }

    public void Save()
    {
      XmlUtil.SerializerObjToFile(this);
    }
  }
}
