using System;
using System.Windows.Input;
using MarketData.Service;
using Navigator.Core;
using System.ComponentModel;
using Navigator.Utils;

namespace Navigator.ViewModels
{
    public class AboutViewModel : BaseViewModel
    {
      private String message = "";
      private String status="";
      private String ipAddress;
      private RelayCommand ipAddressCommand;
      private RelayCommand onResetCommand;

      public AboutViewModel()
      {
        Title = "About";
        Message=MarketDataServiceClient.GetInstance().GetBaseUri();
        Status="";
        PropertyChanged+=OnViewModelPropertyChanged;
      }
      private void OnViewModelPropertyChanged(object sender,PropertyChangedEventArgs eventArgs)
      {
      }
      public ICommand IPAddressCommand
      {
        get
        {
          if(null==ipAddressCommand)
          {
            ipAddressCommand=new RelayCommand(param=>
            {
              HandleIPAddress();
            },param=>{return true;});
          }
          return ipAddressCommand;
        }
      }
      public ICommand OnResetCommand
      {
        get
        {
          if(null==onResetCommand)
          {
            onResetCommand=new RelayCommand(param=>
            {
              HandleReset();
            },param=>{return true;});
          }
          return onResetCommand;
        }
      }

      public void HandleIPAddress()
      {
        if(String.IsNullOrEmpty(ipAddress))return;
        String url=ToUrl(ipAddress);
        Status="ping "+url+" ...";
        if(MarketDataServiceClient.GetInstance().Ping(url))
        {
          MarketDataServiceClient.GetInstance().SetUrl(url,AppSettingsManager.Settings["ServiceUser"]);
          IPCache.SaveIPAddress(ipAddress);
          Status ="ping succeeded";
        }
        else Status="ping failed";
        Message=MarketDataServiceClient.GetInstance().GetBaseUri();
      }

      public void HandleReset()
      {
        Status="";
        Status="ping "+ToUrl(AppSettingsManager.Settings["Service_Local"])+" ...";
        IPAddress="";
        if(MarketDataServiceClient.IsWiFiNetwork() && MarketDataServiceClient.GetInstance().Ping(ToUrl(AppSettingsManager.Settings["Service_Local"])))
        {
          MarketDataServiceClient.GetInstance().SetUrl(ToUrl(AppSettingsManager.Settings["Service_Local"]),AppSettingsManager.Settings["ServiceUser"]);
          Message=ToUrl(AppSettingsManager.Settings["Service_Local"]);
          IPCache.SaveIPAddress(AppSettingsManager.Settings["Service_Local"]);
          Status="ping succeeded, connection reset to "+ToUrl(AppSettingsManager.Settings["Service_Local"]);
          return;
        }
        Status="ping "+ToUrl(AppSettingsManager.Settings["Service_Network"])+" ...";
        if(MarketDataServiceClient.GetInstance().Ping(ToUrl(AppSettingsManager.Settings["Service_Network"])))
        {
          MarketDataServiceClient.GetInstance().SetUrl(ToUrl(AppSettingsManager.Settings["Service_Network"]),AppSettingsManager.Settings["ServiceUser"]);
          Message=ToUrl(AppSettingsManager.Settings["Service_Network"]);
          IPCache.SaveIPAddress(AppSettingsManager.Settings["Service_Network"]);
          Status="ping succeeded, connection reset to "+ToUrl(AppSettingsManager.Settings["Service_Network"]);
        }
        else Status="ping failed";
      }

      public String Message
      {
        get { return message; }
        set { SetProperty(ref message, value); }
      }

      public String Status
      {
        get{return status;}
        set{SetProperty(ref status,value);}
      }

      public String IPAddress
      {
        get{return ipAddress;}
        set{SetProperty(ref ipAddress,value);}

      }

      private String ToUrl(String ipAddress)
      {
        return AppSettingsManager.Settings["Service_Prefix"] + ipAddress + AppSettingsManager.Settings["Service_Postfix"];
      }

   }
}