package us.codecraft.webmagic;

import us.codecraft.webmagic.utils.Experimental;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Object contains url to crawl.<br>
 * It contains some additional information.<br>
 *
 * @author code4crafter@gmail.com <br>
 * @since 0.1.0
 */
public class Request implements Serializable {

    private static final long serialVersionUID = 2062192774891352043L;

    public static final String CYCLE_TRIED_TIMES = "_cycle_tried_times";
    public static final String STATUS_CODE = "statusCode";
    public static final String PROXY = "proxy";

    private String url;

    private String method;

    /**
     * Store additional information in extras.
     */
    private Map<String, Object> extras;
    /**
     * POST/GET param set
     * */
    private Map<String,String> params=new HashMap<String, String>();

    /**
     * Priority of the request.<br>
     * The bigger will be processed earlier. <br>
     * @see us.codecraft.webmagic.scheduler.PriorityScheduler
     */
    private long priority;

    public Request() {
    }

    public Request(String url) {
        this.url = url;
    }

    public long getPriority() {
        return priority;
    }

    /**
     * Set the priority of request for sorting.<br>
     * Need a scheduler supporting priority.<br>
     * @see us.codecraft.webmagic.scheduler.PriorityScheduler
     *
     * @param priority priority
     * @return this
     */
    @Experimental
    public Request setPriority(long priority) {
        this.priority = priority;
        return this;
    }

    public Object getExtra(String key) {
        if (extras == null) {
            return null;
        }
        return extras.get(key);
    }

    public Request putExtra(String key, Object value) {
        if (extras == null) {
            extras = new HashMap<String, Object>();
        }
        extras.put(key, value);
        return this;
    }

    public String getUrl() {
        return url;
    }

    public Map<String, Object> getExtras() {
        return extras;
    }

    public void setExtras(Map<String, Object> extras) {
        this.extras = extras;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * The http method of the request. Get for default.
     * @return httpMethod
     * @see us.codecraft.webmagic.utils.HttpConstant.Method
     * @since 0.5.0
     */
    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public Map<String, String> getParams() {
        return params;
    }
    /**
     * POST/GET参数设置
     * */
    public void setParams(Map<String, String> params) {
        this.params = params;
    }
    /**
     * POST/GET参数设置
     * */
    public void putParams(String key,String value) {
        params.put(key,value);
    }
    @Override
    public String toString() {
        return "Request{" +
                "url='" + url + '\'' +
                ", method='" + method + '\'' +
                ", extras=" + extras +
                ", priority=" + priority +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Request request = (Request) o;

        if (url != null ? !url.equals(request.url) : request.url != null) return false;
        return method != null ? method.equals(request.method) : request.method == null;
    }

    @Override
    public int hashCode() {
        int result = url != null ? url.hashCode() : 0;
        result = 31 * result + (method != null ? method.hashCode() : 0);
        return result;
    }
}

@@ -2650,32 +2650,60 @@
  * POST/GET%E5%8F%82%E6%95%B0%E8%AE%BE%E7%BD%AE%0A
+     * @param params params%0A
      * */%0A    pu
@@ -2812,24 +2812,72 @@
 OST/GET%E5%8F%82%E6%95%B0%E8%AE%BE%E7%BD%AE%0A
+     * @param key key%0A     * @param value value%0A
      * */%0A  
@@ -2967,276 +2967,8 @@
    %7D
-%0A    @Override%0A    public String toString() %7B%0A        return %22Request%7B%22 +%0A                %22url='%22 + url + '%5C'' +%0A                %22, method='%22 + method + '%5C'' +%0A                %22, extras=%22 + extras +%0A                %22, priority=%22 + priority +%0A                '%7D';%0A    %7D
 %0A%0A  
@@ -3253,31 +3253,28 @@
 se;%0A        
-return 
+if (
 method != nu
@@ -3274,24 +3274,25 @@
 d != null ? 
+!
 method.equal
@@ -3326,16 +3326,118 @@
 .method 
+!= null) return false;%0A        return params != null ? params.equals(request.params) : request.params 
 == null;
@@ -3628,19 +3628,400 @@
   re
-turn result
+sult = 31 * result + (params != null ? params.hashCode() : 0);%0A        return result;%0A    %7D%0A%0A    @Override%0A    public String toString() %7B%0A        return %22Request%7B%22 +%0A                %22url='%22 + url + '%5C'' +%0A                %22, method='%22 + method + '%5C'' +%0A                %22, extras=%22 + extras +%0A                %22, params=%22 + params +%0A                %22, priority=%22 + priority +%0A                '%7D'
 ;%0A  

rewrite hashCode and equals for params #447