package com.wasu.cs.mvp.presenter;

import android.text.TextUtils;

import com.wasu.common.WError;
import com.wasu.cs.model.CatData;
import com.wasu.cs.model.ChildrenAllLikeData;
import com.wasu.cs.model.ChildrenData;
import com.wasu.cs.mvp.IView.IChildrenAllLikeView;
import com.wasu.cs.protocol.CatProtocol;
import com.wasu.module.datafetch.DataFetchListener;
import com.wasu.module.datafetch.DataFetchModule;
import com.wasu.module.datafetch.ObjectBase;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by jeepc on 2016/10/24.
 */

public class ChildrenAllLikePresenter {
    String json = "{code:200, data:{bgPicUrl:\"\",list:[ {jsonUrl:\"http://120.26.137.228?s=2002&p=sntAssetList&k=1&v=3&catId=220166&order=1\",layout:\"ShortVideo_List\",linkType:-10,title:\"财智通\"},{ jsonUrl:\"http://120.26.137.228?s=2002&p=sntAssetList&k=1&v=3&catId=220165&order=1\",layout:\"ShortVideo_List\",linkType:-10,title:\"名嘴观点\"},{jsonUrl:\"http://120.26.137.228?s=2002&p=sntAssetList&k=1&v=3&catId=220163&order=1\", layout:\"ShortVideo_List\",linkType:-10,title:\"国内视野\"},{jsonUrl:\"http://120.26.137.228?s=2002&p=sntAssetList&k=1&v=3&catId=220162&order=1\", layout:\"ShortVideo_List\", linkType:-10,title:\"社会百态\"}]},message:\"请求成功\"}";
    private IChildrenAllLikeView view;

    public void attachView(IChildrenAllLikeView view) {
        this.view = view;
    }

    public void detachView() {
        if (view != null) {
            view = null;
        }
    }


    public void loadData(final String url) {
        if(TextUtils.isEmpty(url))return;
        if (view != null) {
            view.showLoading();
        }
        DataFetchModule.getInstance().fetchObjectGet(url, ChildrenAllLikeData.class, new DataFetchListener.ObjectListener() {

            @Override
            public void onObjectGet(int retcode, String extraMsg, ObjectBase data) {
                if (retcode == WError.ERROR_NONE) {
                    if (view != null && !view.uiIsFinishing()) {
                        ChildrenAllLikeData childrenData = (ChildrenAllLikeData) data;
                        if (data != null) {
                            view.setBg(childrenData.getBgPicUrl());
                            view.hideLoading();
                            view.onDataLoaded(childrenData);
                        }
                    }
                } else {
                    if (view != null) {
                        view.hideLoading();
                        view.showErrorExitDlg();
                    }
                }
            }
        });

    }

}
