package com.ifish.gwell;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ifish.enums.GwellEnum;
import com.ifish.mail.Mail;
import com.ifish.netease.CheckSumBuilder;

public class GwellApi {
	
	private static Logger log = LoggerFactory.getLogger(GwellApi.class);
	
	/**
	 * 注册
	 * @param phoneNumber
	 * @return
	 */
	public static Map<String,String> Register(String phoneNumber){
		String email = getEmail(phoneNumber);
		String pwd = getPwd(phoneNumber);
		Map<String,String> map = new HashMap<String, String>();
		URL url = null;
		HttpURLConnection httpurlconnection = null;
		try{
			if(url==null){
				url = new URL("http://api1.cloudlinks.cn/Users/RegisterCheck.ashx");
			}
			if(httpurlconnection==null){
				//以post方式请求
				httpurlconnection = (HttpURLConnection)url.openConnection();
				httpurlconnection.setReadTimeout(30000);
				httpurlconnection.setConnectTimeout(30000);
				//是否输入参数
				httpurlconnection.setDoOutput(true);
				//提交模式
				httpurlconnection.setRequestMethod("POST");
			}
			StringBuffer params = new StringBuffer();
			params.append("Email="+email+"&");
			params.append("Pwd="+pwd+"&");
			params.append("RePwd="+pwd+"&");
			params.append("VersionFlag=1&");
			params.append("CountryCode=&");
			params.append("IgnoreSafeWarning=1");
			OutputStream out = httpurlconnection.getOutputStream();
			out.write(params.toString().getBytes());
			out.flush();
			out.close();
			//获取响应代码 
			int code = httpurlconnection.getResponseCode();
			if(code==200){
				//获取页面内容
				InputStream in= httpurlconnection .getInputStream();
				BufferedReader breader = new BufferedReader(new InputStreamReader(in,"utf-8"));
				String jsonStr="";
				String str = breader.readLine();
				while(str != null){
					jsonStr+=str;
					str = breader.readLine();
				}
				breader.close();
				in.close();
				//System.out.println(jsonStr);
				JSONObject json = JSON.parseObject(jsonStr);
				String error_code = json.getString("error_code");
				//注册成功
				if(error_code.equals("0")){
					String P2PVerifyCode1 = json.getString("P2PVerifyCode1");
					String P2PVerifyCode2 = json.getString("P2PVerifyCode2");
					String UserID = json.getString("UserID");
					map.put("result", GwellEnum.success.getKey());
					map.put("P2PVerifyCode1", P2PVerifyCode1);
					map.put("P2PVerifyCode2", P2PVerifyCode2);
					map.put("UserID", UserID);
					return map;
				}
				//邮箱已被注册
				else if(error_code.equals("7")){
					String error = json.getString("error");
					map.put("result", GwellEnum.repeat.getKey());
					map.put("error", error);
				}
				//失败
				else if(error_code.equals("1")){
					//发送邮件
					Mail.send(phoneNumber+"注册技威失败了", "注册技威失败!错误信息:"+jsonStr);
					String error = json.getString("error");
					map.put("result", GwellEnum.fail.getKey());
					map.put("error", error);
					return map;
				}
			}
		}
		catch(Exception e){
			e.printStackTrace();
			log.error(e.toString());
		}
		finally{
			if(httpurlconnection!=null){
				httpurlconnection.disconnect();
			}
		}
		return map;
	}
	
	
	/**
	 * 登陆
	 * @param phoneNumber
	 * @return
	 */
	public static Map<String,String> Login(String phoneNumber){
		String email = getEmail(phoneNumber);
		String pwd = getPwd(phoneNumber);
		Map<String,String> map = new HashMap<String, String>();
		URL url = null;
		HttpURLConnection httpurlconnection = null;
		try{
			if(url==null){
				url = new URL("http://api1.cloudlinks.cn/Users/LoginCheck.ashx");
			}
			if(httpurlconnection==null){
				//以post方式请求
				httpurlconnection = (HttpURLConnection)url.openConnection();
				httpurlconnection.setReadTimeout(30000);
				httpurlconnection.setConnectTimeout(30000);
				//是否输入参数
				httpurlconnection.setDoOutput(true);
				//提交模式
				httpurlconnection.setRequestMethod("POST");
			}
			StringBuffer params = new StringBuffer();
			params.append("User="+email+"&");
			params.append("Pwd="+pwd+"&");
			params.append("VersionFlag=1&");
			params.append("AppVersion=3014666&");
			params.append("AppOS=3");
			OutputStream out = httpurlconnection.getOutputStream();
			out.write(params.toString().getBytes());
			out.flush();
			out.close();
			//获取响应代码 
			int code = httpurlconnection.getResponseCode();
			if(code==200){
				//获取页面内容
				InputStream in= httpurlconnection .getInputStream();
				BufferedReader breader = new BufferedReader(new InputStreamReader(in,"utf-8"));
				String jsonStr="";
				String str = breader.readLine();
				while(str != null){
					jsonStr+=str;
					str = breader.readLine();
				}
				breader.close();
				in.close();
				//System.out.println(jsonStr);
				JSONObject json = JSON.parseObject(jsonStr);
				String error_code = json.getString("error_code");
				if(error_code.equals("0")){
					String P2PVerifyCode1 = json.getString("P2PVerifyCode1");
					String P2PVerifyCode2 = json.getString("P2PVerifyCode2");
					String UserID = json.getString("UserID");
					map.put("result", GwellEnum.success.getKey());
					map.put("P2PVerifyCode1", P2PVerifyCode1);
					map.put("P2PVerifyCode2", P2PVerifyCode2);
					map.put("UserID", UserID);
					return map;
				}
				else{
					String error = json.getString("error");
					map.put("result", GwellEnum.fail.getKey());
					map.put("error", error);
					return map;
				}
			}
		}
		catch(Exception e){
			log.error(e.toString());
		}
		finally{
			if(httpurlconnection!=null){
				httpurlconnection.disconnect();
			}
		}
		return map;
	}
	
	/**
	 * 获取用户名
	 * @param phoneNumber
	 * @return
	 */
	public static String getEmail(String phoneNumber){
		String email = "ifish7"+phoneNumber+"@ifish7.com";
		return email;
	}
	/**
	 * 获取密码
	 * @param phoneNumber
	 * @return
	 */
	public static String getPwd(String phoneNumber){
		String pwd = CheckSumBuilder.getMD5("ifish"+phoneNumber);
		return pwd;
	}
	
	
	public static void main(String[] args) {
		Register("15216102193");
	}
	
}
