package com.wasu.cs.ui;

import android.os.Bundle;
import android.view.View;

import com.wasu.cs.model.CatData;
import com.wasu.cs.model.Model;
import com.wasu.cs.module.WasuCacheModule;
import com.wasu.cs.protocol.BaseListProtocol;
import com.wasu.cs.protocol.CatProtocol;

/**
 * Created by LiuYang on 7/7/15.
 */
public abstract class FragmentPageBase extends FragmentChannelBase {

    CatProtocol mProtocol;
    CatData mCatData;
    String mLayoutCode;
    String mJsonUrl;
    protected int mPosition = -1;
    private static final int CACHE_LIFE_CYCLE = 3*3600;//缓存的生命周期，单位秒

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mProtocol = new CatProtocol();
        mJsonUrl = getArguments().getString("jsonUrl");
        mProtocol.withUrl(mJsonUrl).withPageSize(100);
        mLayoutCode = getArguments().getString("layout");
        mPosition = getArguments().getInt("position", -1);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    public CatProtocol getProtocol() {
        return mProtocol;
    }

    public String getJsonUrl() {
        return mJsonUrl;
    }

    public String getLayoutCode() {
        return mLayoutCode;
    }

    public void fetchData() {
        if (0 == mPosition && mLayoutCode.contains("Recommend")) {
            CatData data = (CatData) WasuCacheModule.getInstance().getAsObject(mProtocol.getRequestUrl());
            if(data != null){
	            onFetchData(data);//回调缓存的数据
	            mProtocol.fetchData(getHandler(), mProtocol.getRequestUrl(), new BaseListProtocol.DataFetchCallback() {
                    @Override
                    public void onResult(boolean successed, Model data) {
                        if (successed) {
                            mCatData = (CatData) data;
                            onFetchData(mCatData);//回传最新的数据
	                        WasuCacheModule.getInstance().remove(mProtocol.getRequestUrl());
                            WasuCacheModule.getInstance().put(mProtocol.getRequestUrl(), mCatData,CACHE_LIFE_CYCLE);
                        } else {

                        }
                    }
                });
            }else{
                mProtocol.fetchData(getHandler(), mProtocol.getRequestUrl(), new BaseListProtocol.DataFetchCallback() {
                    @Override
                    public void onResult(boolean successed, Model data) {
                        if (successed) {
                            mCatData = (CatData) data;
                            onFetchData(mCatData);
                            WasuCacheModule.getInstance().put(mProtocol.getRequestUrl(), mCatData,CACHE_LIFE_CYCLE);
                        } else {

                        }
                    }
                });
            }
        }else {
            mProtocol.fetchData(getHandler(), mProtocol.getRequestUrl(), new BaseListProtocol.DataFetchCallback() {
                @Override
                public void onResult(boolean successed, Model data) {
                    if (successed) {
                        mCatData = (CatData) data;
                        onFetchData(mCatData);
                    } else {

                    }
                }
            });
        }
    }

    public void onSelected() {
    }

    public void onUnSelected() {
    	
    }
    
    public abstract void onFetchData(CatData catData);
	public View focusSearch(View focused, int direction) {
    	/**加上判断，如果是视频小带有视频小窗口的二级列表设置焦点在右边的listview向下按无反应**/
    	if(focused.getClass().getSimpleName().equals("FocusGridViewEx")&&direction == View.FOCUS_DOWN && FragmentFactory.SHORT_VIDEO_LIST.equals(mLayoutCode)){
    		return focused;
    	}else{
        return getView();
    	}
    }
}
