/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jmb_g1;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

/**
 *
 *
 */
public class MemberModel {

    public static void memberCheck(String name, String password)
            throws Exception {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(
                    "https://d...content-available-to-author-only...e.com/join/member_check.php");
            java.util.List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("user_id", name));
            params.add(new BasicNameValuePair("password", password));
            params.add(new BasicNameValuePair("s_url",
                    "http://w...content-available-to-author-only...e.com/"));

            httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

            httpPost.setHeader("Content-Type",
                    "application/x-www-form-urlencoded; charset=UTF-8");
            httpPost.setHeader("Host", "dcid.dcinside.com");
            httpPost.setHeader("Referer", "http://w...content-available-to-author-only...e.com/");
            httpPost.setHeader("Origin", "http://w...content-available-to-author-only...e.com");
            httpPost.setHeader(
                    "User-Agent",
                    "Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166  Safari/535.19");

            HttpResponse response = httpclient.execute(httpPost);
            HttpEntity entity = response.getEntity();

            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent(), "UTF-8"));

            String t;
            while ((t = bf.readLine()) != null) {
                Controller.result.add(t);
            }
            if (Controller.result.contains("				history.back(-1);")) {
                //System.out.println("** 濡쒓렇???ㅽ뙣");
                String reason = Controller.result.get(25);
                String reason2[] = reason.split("'");
                //System.out.println("** ?댁쑀 : " + reason2[1]);
                Controller.state = reason2[1];

            } else {
                //System.out.println("** 濡쒓렇?몄뿉 ?깃났?덉뒿?덈떎.");
                Controller.state = "login_ok";
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("** ?쒕쾭?먯꽌 ?묐떟??嫄곕??덉뒿?덈떎.");
            System.out.println("** 李⑤떒??媛ㅻ줈洹몄씪 ???덉뒿?덈떎.");
			// JMBCore.resultLog("ArrayIndexOutOfBoundsException",
            // "memberCheck");

        }
    } // memberCheck end

    public static void gnoGet(String name) throws Exception {
        try {
            Document doc = (Document) Jsoup
                    .connect(
                            "http://m...content-available-to-author-only...e.com/gallog/home.php?g_id="
                            + name)
                    .userAgent(
                            "Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166  Safari/535.19")
                    .get();
            Elements gnoGet = doc.select(".list_picture li");
            String[] gnoProcess1 = gnoGet.html().split("g_no=");
            String[] gnoProcess2 = gnoProcess1[1].split("\">");
            int gno = Integer.parseInt(gnoProcess2[0]);
            Controller.gno = gno + 1;
        } catch (Exception e) {
            gnoGet(name);

        }
    }
    
    
     public static int end;
    

 

    public static void getGallogCode() throws Exception {
        try {
            Document doc = Jsoup.connect(
                    "http://g...content-available-to-author-only...e.com/" + Controller.id).get();
            String htmlsource = doc.toString();
            String[] htmlSource2 = htmlsource.split("profile_no=");
            String[] htmlSource3 = htmlSource2[1].split("'");
            Controller.gallogCode = htmlSource3[0];

        } catch (Exception e) {
            getGallogCode();
        }
    }

    public static void getMemberName() throws Exception {
        try {
            Document doc = Jsoup
                    .connect(
                            "http://m...content-available-to-author-only...e.com/gallog/home.php?g_id="
                            + Controller.id)
                    .userAgent(
                            "Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166  Safari/535.19")
                    .get();
            Elements count = doc.select(".title_gallog_info");
            String[] gallogName = count.text().split("]");
            Controller.name = gallogName[0].substring(1);
        } catch (Exception e) {
            getMemberName();
        }
    }

    public static void getMemberCount() throws Exception {
        try {
            Document doc = Jsoup
                    .connect(
                            "http://m...content-available-to-author-only...e.com/gallog/home.php?g_id="
                            + Controller.id)
                    .userAgent(
                            "Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166  Safari/535.19")
                    .get();
            Elements count = doc.select(".gall_write");
            Elements countReply = doc.select(".gall_reply");
            String[] getArticle = count.text().split("湲 ");
            String[] getReply = countReply.text().split("湲 ");

            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < getArticle[1].length(); i++) {
                if (Character.isDigit(getArticle[1].charAt(i))) {
                    sb.append(getArticle[1].charAt(i));
                }
            }
            String aResult = sb.toString();

            StringBuffer sb2 = new StringBuffer();
            for (int i = 0; i < getReply[1].length(); i++) {
                if (Character.isDigit(getReply[1].charAt(i))) {
                    sb2.append(getReply[1].charAt(i));
                }
            }
            String rResult = sb2.toString();

            Controller.articleCount = Integer.parseInt(aResult);
            Controller.replyCount = Integer.parseInt(rResult);

            Controller.max = Controller.articleCount + Controller.replyCount;

            if (Controller.max == 0) {
                System.out.println("** 吏?멸쾶 ?놁쓬.");
                
                getMemberName();
            }

        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("** ?쒕쾭?먯꽌 ?묐떟??嫄곕??덉뒿?덈떎.");
            System.out.println("** 李⑤떒??媛ㅻ줈洹몄씪 ???덉뒿?덈떎.");
        } catch (Exception e) {
            getMemberCount();
        }
    }



    
}
