using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace ShopBot.Core
{
    class Post
    {
        public static string DoIt(string Mode,string ConnectionParameters)
        {
            WebRequest PanelRequest;

            try
            {
                PanelRequest = WebRequest.Create(Configuration.ShopPanel + Mode);
                PanelRequest.Timeout = 30000;
            }
            catch
            {
                PanelRequest = WebRequest.Create(Configuration.BackupShopPanel);
            }

            PanelRequest.ContentType = "application/x-www-form-urlencoded";
            PanelRequest.Method = "POST";
            byte[] bytes = Encoding.ASCII.GetBytes(ConnectionParameters);
            Stream os = null;
            try
            {
                PanelRequest.ContentLength = bytes.Length;
                os = PanelRequest.GetRequestStream();
                os.Write(bytes, 0, bytes.Length);
            }
            catch
            {
            }
            finally
            {
                if (os != null)
                {
                    os.Close();
                }
            }

            try
            {
                WebResponse webResponse = PanelRequest.GetResponse();
                if (webResponse == null)
                { return null; }
                StreamReader sr = new StreamReader(webResponse.GetResponseStream());
                return sr.ReadToEnd().Trim();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return null;
        }
    }
}
