package com.wasu.cs.protocol;

import org.json.JSONObject;


import com.wasu.cs.model.Model;
import com.wasu.cs.model.SearchModel;
import com.wasu.module.datafetch.DataFetchListener.JsonListener;
import com.wasu.module.datafetch.DataFetchModule;

public class SearchProtocol extends BaseProtocol {

	public void fetch(String url, final ProtocolCallbackBase callback) {
		DataFetchModule.getInstance().fetchJsonGet(url, new JsonListener() {
			
			@Override
			public void onJsonGet(int retcode, String extraMsg, JSONObject jsonObj) {
				if (null == jsonObj) {
					callback.onProtocolCallback(false, "protocol return data is null", null);
				} else {
					SearchModel searchModel = new SearchModel();
					if (searchModel.from(jsonObj)) {
						callback.onProtocolCallback(true, null, searchModel);
					} else {
						callback.onProtocolCallback(false, "parser result data fail", null);
					}
				}
			}
		});
	}
	
	public static interface ProtocolCallbackBase {
		void onProtocolCallback(boolean success, String msg, Model model);
	}
}
