package com.wasu.cs.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.SparseArray;

import cn.com.wasu.main.LayoutCodeMap;

/**
 * 二级列表页fragment构造器
 * 根据服务器返回的不同的layoutcode来创建对应类型的fragment
 * 目前有只有两种fragment，包含视频小窗口的和不包含视频小窗口的
 * Created by LiuYang on 6/25/15.
 */
public class FragmentFactory {

    public final static String SHORT_VIDEO_LIST = "ShortVideo_List";
    public final static String SHORT_VIDEO_LIST_ONE = "ShortVideo_List1";

    //private static HashMap<String,Fragment> fragments = new HashMap<String, Fragment>();
    private static SparseArray<Fragment> fragments = new SparseArray<>();
    public static Fragment buildFragment(Bundle bundle) {
        Fragment fragment = null;

        String layoutCode = bundle.getString("layout");
        String jsonUrl = bundle.getString("jsonUrl");
        int pos = bundle.getInt("position");

       // fragment = fragments.get(jsonUrl);
        fragment = fragments.get(pos);
        if(fragment == null) {
            if(layoutCode.equalsIgnoreCase(LayoutCodeMap.RECOMMEND_MOVIE)
                    || layoutCode.equalsIgnoreCase(LayoutCodeMap.NEWS_LIST)
                    || layoutCode.equalsIgnoreCase(LayoutCodeMap.MOVIE_LIST)) {
                fragment = new FragmentChannelMovieHome();
            }
            else if(layoutCode.equalsIgnoreCase(SHORT_VIDEO_LIST)) {
                fragment = new FragmentChannelNewsList();
            }else if(layoutCode.equalsIgnoreCase(SHORT_VIDEO_LIST_ONE)){
            	fragment = new FragmentChannelShortVideoList();
            }
            else {
                fragment = new FragmentChannelMovieHome();
            }

            fragments.put(pos, fragment);
            fragment.setArguments(bundle);
        }


        return fragment;
    }

    public static void clearCache(){
       /* Collection<Fragment> values = fragments.values();
        for (Fragment fragment : values) {
            if (fragment instanceof FragmentChannelNewsList) {
                fragment.setUserVisibleHint(false);
            }
        }
    	fragments.clear();*/

        SparseArray<Fragment> values=fragments;
        for (int i=0,count=fragments.size();i<count;i++){
            if (values.get(i) instanceof FragmentChannelNewsList){
                values.get(i).setUserVisibleHint(false);
            }
        }
        values.clear();
    }
    
}
