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

namespace jpetJBOARD
{
    class clsJpet
    {
        CookieContainer cookie;

        public clsJpet()
        {
            cookie = new CookieContainer();
        }
        
        public Boolean Login(String ID, String Passwd)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://jpet.jboard.net/mb/mb_logincheck.php");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = cookie;

            StreamWriter sw = new StreamWriter(request.GetRequestStream());
            sw.Write("ID=" + ID + "&Passwd=" + Passwd + "&mbM=login");
            sw.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream stream = response.GetResponseStream();
                StreamReader readStream = new StreamReader(stream, Encoding.Default);
                String result = readStream.ReadToEnd();

                if (result.IndexOf("잘못") < 0) return true;
            }
            return false;
        }

        private String StripTagsCharArray(String source)
        {
            Char[] array = new Char[source.Length];
            Int32 arrayIndex = 0;
            Boolean inside = false;

            for (Int32 i = 0; i < source.Length; i++)
            {
                Char let = source[i];
                if (let == '<') { inside = true; continue; }
                if (let == '>') { inside = false; continue; }
                if (!inside) { array[arrayIndex] = let; arrayIndex++; }
            }
            return new String(array, 0, arrayIndex);
        }

        public List<String[]> jshopInfo(String bgID)
        {
            List<String[]> jshopInfo = new List<String[]>();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://jpet.jboard.net/jshop/jshop.php");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = cookie;

            StreamWriter sw = new StreamWriter(request.GetRequestStream());
            sw.Write("bg_id=" + bgID + "&jbM=order_v");
            sw.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream stream = response.GetResponseStream();
                StreamReader readStream = new StreamReader(stream, Encoding.Default);
                String result = readStream.ReadToEnd();
                String[] jshopList = Split(Split(result, "상품주문상세정보")[1], "<a href='/jshop/jshop.php");

                for (int i = 1; i < jshopList.Length; i++)
                {
                    String[] info = new String[3];
                    info[0] = Split(Split(jshopList[i], "&tite=")[1], "'")[0];
                    info[1] = Split(Split(jshopList[i], "#FFFFFF\">")[1], "<")[0];
                    info[2] = Split(Split(jshopList[i], "width=\"40\">")[1], "<")[0].Trim();
                    jshopInfo.Add(info);
                }
            }

            return jshopInfo;
        }

        public List<String[]> orderList()
        {
            List<String[]> orderList = new List<String[]>();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://jpet.jboard.net/jshop/jshop.php?jbM=order_l");
            request.CookieContainer = cookie;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream stream = response.GetResponseStream();
                StreamReader readStream = new StreamReader(stream, Encoding.Default);
                String result = readStream.ReadToEnd();
                String[] orderInfo = Split(Split(result, "name=jshop_order_search")[1], "javascript:jshop_order_search");
                for (int i = 1; i <= orderInfo.Length - 1; i++)
                {
                    String[] info = new String[8];
                    info[0] = Split(Split(orderInfo[i], "color=blue>")[1], "<")[0];
                    info[1] = Split(Split(orderInfo[i], "<td>")[1], "<")[0].Trim();
                    info[2] = Split(Split(orderInfo[i], "009900\">")[1], "<")[0].Trim();
                    info[3] = Split(Split(orderInfo[i], "FC6703\">")[1], "<")[0].Trim();
                    info[4] = Split(Split(orderInfo[i], "0066FF\">")[1], "<")[0].Trim();
                    info[7] = Split(orderInfo[i], "'")[1];
                    String[] tmp = Split(StripTagsCharArray(Split(Split(orderInfo[i], "<td>")[5], "</td>")[0].Trim()), " ");
                    
                    if (tmp.Length == 2) { info[5] = tmp[0]; info[6] = tmp[1]; }
                    else { info[5] = info[6] = "EMPTY"; }
                    orderList.Add(info);
                }
            }
            return orderList;
        }

        private String[] Split(String body, String separator) { return body.Split(new String[] { separator }, StringSplitOptions.RemoveEmptyEntries); }
    }
}