package com.wasu.cs.protocol;

import android.os.Handler;

import com.wasu.cs.model.CatData;

import org.json.JSONObject;


/**
 * 栏目协议
 * @author LiuYang
 * Created by LiuYang on 7/2/15.
 */
public class CatProtocol extends BaseListProtocol {
    private CatData data = new CatData();

    @Override
    public boolean from(JSONObject json) {
        boolean result = super.from(json);

        if (result) {
            data.from(json);
        }

        return result;
    }

    @Override
    public int getTotalSize() {
        return data.getTotal();
    }

    @Override
    public CatData getData() {
        return data;
    }

    /**
     * 数据获取，在主线程回调
     * @param handler 回调句柄
     * @param url 请求地址
     * @param callback 回调
     */
    public static void fetchData(final Handler handler,String url,final CatFetchCallback callback) {
        CatProtocol catProtocol = new CatProtocol();
        catProtocol.withPageSize(50).withUrl(url);
        fetch(catProtocol.getRequestUrl(), new FetchCallback() {
            @Override
            public void onResult(final boolean successed, final BaseProtocol protocol) {
                callback.onResult(successed, successed ? ((CatProtocol) protocol).getData():null);
            }
        }, catProtocol);
    }

    public interface CatFetchCallback {
        public void onResult(boolean successed, CatData catData);
    }
    
    public void cleanData(){
    	if(data != null){
    		data = null;
    	}
    }

}
