package com.wasu.cs.adapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import com.wasu.cs.model.CatData.Cat;
import com.wasu.cs.ui.FragmentChannelMovieHome;
import com.wasu.cs.ui.FragmentFactory;
import com.wasu.cs.widget.FocusGridViewEx;

import cn.com.wasu.main.BuildConfig;
import cn.com.wasu.main.ChannelFlavor;

/**
 * 4.0二级列表页的container部分的适配器
 * 使用fragment构造器（FragmentFactory）来获取对应的fragment
 * Created by LiuYang on 7/6/15.
 */
public class ChannelHomeAdapter extends BlockPageAdapter {

    private FocusGridViewEx.FocusGridViewColumnListener mOnGridClumnListener;

    public ChannelHomeAdapter(FragmentManager fm) {
        super(fm);
    }
    /**
     * 根据layout和jsonurl创建对应的fragment
     */
    @Override
    public Fragment getItem(int position) {
        Bundle bundle = new Bundle();
        Cat cat = (Cat) getItemData(position);
        if(null == cat){
        	return null;
        }else{
        	 bundle.putString("layout",cat.getLayout());
             bundle.putString("jsonUrl",cat.getJsonUrl());
             bundle.putInt("position", position);
             Fragment fragment = FragmentFactory.buildFragment(bundle);

            if (BuildConfig.FLAVOR.equalsIgnoreCase(ChannelFlavor.LETV_MARKET) && fragment instanceof FragmentChannelMovieHome){
                ((FragmentChannelMovieHome)fragment).setOnGridClumnListener(mOnGridClumnListener);
            }
             return FragmentFactory.buildFragment(bundle);
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        String res = "";
        Cat cat = (Cat) getItemData(position);
        if(cat != null) {
            res = cat.getName();
        }
        return res;
    }

    public void setOnGridClumnListener(FocusGridViewEx.FocusGridViewColumnListener onGridClumnListener) {
        this.mOnGridClumnListener = onGridClumnListener;
    }
}
