using System.Text;
using MarketData.Utils;

namespace IPMonitor
{
  public class ZoneEditResponses : List<ZoneEditResponse>
  {
    public ZoneEditResponses()
    {
    }

    public bool IsSuccess()
    {
      if(Count==0)return false;
      return this.Any(x => x.SuccessCode.Equals("200") || x.SuccessCode.Equals("201"));
    }

    public override string ToString()
    {
      StringBuilder sb=new StringBuilder();
      for(int index=0;index<Count;index++)
      {
        ZoneEditResponse zoneEditResponse=this[index];
        sb.Append(zoneEditResponse).ToString();
        if(index<Count-1)sb.Append("\n");
      }
      return sb.ToString();
    }

    public ZoneEditResponses(String httpResponseString)
    {
      if(null==httpResponseString)return;
      String[] responseItems = httpResponseString.Split('\n');
      foreach(String item in responseItems)
      {
        int successCodeIndex= item.IndexOf("SUCCESS CODE=");
        int textIndex=item.IndexOf("TEXT=");
        int zoneIndex=item.IndexOf("ZONE=");
        if(-1==successCodeIndex || -1==textIndex || -1==zoneIndex)continue;
        String successCode=Utility.BetweenString(item.Substring(successCodeIndex),"\"","\"");
        String text=Utility.BetweenString(item.Substring(textIndex),"\"","\"");
        String zone=Utility.BetweenString(item.Substring(zoneIndex),"\"","\"");
        ZoneEditResponse zoneEditResponse= new ZoneEditResponse(successCode, text, zone);
        Add(zoneEditResponse);
      }
    }
  }

//		ResponseString	"<SUCCESS CODE=\"201\" TEXT=\"no update required for diversified-software.com to 67.191.114.201\" ZONE=\"diversified-software.com\">\n<SUCCESS CODE=\"200\" TEXT=\"diversified-software.com updated to 67.191.114.201\" ZONE=\"diversified-software.com\">\n"	string

  public class ZoneEditResponse
  {
    public String SuccessCode { get; set; }
    public String Text { get; set; }
    public String Zone { get; set; }

    public ZoneEditResponse()
    {
    }

    public ZoneEditResponse(String successCode, String text, String zone)
    {
      SuccessCode=successCode;
      Text=text;
      Zone=zone;
    }
    
    public override String ToString()
    {
      StringBuilder sb = new StringBuilder();
      sb.Append("SUCCESS CODE=").Append(SuccessCode);
      sb.Append(",");
      sb.Append("TEXT=").Append(Text);
      sb.Append(",");
      sb.Append("ZONE=").Append(Zone);
      return sb.ToString();
    }
  }
}
