/*
 * Copyright 2015 Synced Synapse. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.xbmc.kore.utils;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * This is a helper class that implements the management of tabs and all
 * details of connecting a ViewPager with associated TabHost.
 */
public class TabsAdapter extends FragmentPagerAdapter {
    private final Context context;
    private final ArrayList<TabInfo> tabInfos;

    public static final class TabInfo {
        private final Class<?> fragmentClass;
        private final Bundle args;
        private final int titleRes;
        private final long fragmentId;

        TabInfo(Class<?> fragmentClass, Bundle args, int titleRes, long fragmentId) {
            this.fragmentClass = fragmentClass;
            this.args = args;
            this.titleRes = titleRes;
            this.fragmentId = fragmentId;
        }
    }

    public TabsAdapter(Context context, FragmentManager fragmentManager) {
        super(fragmentManager);
        this.context = context;
        this.tabInfos = new ArrayList<TabInfo>();
    }

    public TabsAdapter addTab(Class<?> fragmentClass, Bundle args, int titleRes, long fragmentId) {
        TabInfo info = new TabInfo(fragmentClass, args, titleRes, fragmentId);
        tabInfos.add(info);
        return this;
    }

    @Override
    public int getCount() {
        return tabInfos.size();
    }

    /**
     * Store the created fragments, so that it is possible to get them by position later
     */
    private HashMap<Integer, Fragment> createdFragments = new HashMap<>(5);

    @Override
    public Fragment getItem(int position) {
        TabInfo info = tabInfos.get(position);
        Fragment fragment = Fragment.instantiate(context, info.fragmentClass.getName(), info.args);

        createdFragments.put(position, fragment);
        return fragment;
    }

    @Override
    public void destroyItem (ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
        createdFragments.remove(position);
    }

    public Fragment getStoredFragment(int position) {
        return createdFragments.get(position);
    }
    @Override
    public long getItemId(int position) {
        return tabInfos.get(position).fragmentId;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        TabInfo tabInfo = tabInfos.get(position);
        if (tabInfo != null) {
//            return context.getString(tabInfo.titleRes).toUpperCase(Locale.getDefault());
            return context.getString(tabInfo.titleRes);
        }
        return null;
    }
}

@@ -2176,24 +2176,223 @@
 e();%0A    %7D%0A%0A
+    @Override%0A    public Fragment getItem(int position) %7B%0A        TabInfo info = tabInfos.get(position);%0A        return Fragment.instantiate(context, info.fragmentClass.getName(), info.args);%0A    %7D%0A%0A
     /**%0A    
@@ -2590,77 +2590,56 @@
 lic 
-Fragment getItem(int position) %7B%0A        TabInfo info = tabInfos.get(
+Object instantiateItem(ViewGroup container, int 
 posi
@@ -2635,33 +2635,34 @@
 r, int position)
-;
+ %7B
 %0A        Fragmen
@@ -2674,24 +2674,25 @@
 gment = 
+(
 Fragment
 .instant
@@ -2675,32 +2675,38 @@
 ment = (Fragment
+)super
 .instantiate(con
@@ -2705,61 +2705,35 @@
 iate
+Item
 (cont
-ext, info.fragmentClass.getName(), info.args
+ainer, position
 );%0A
-%0A
     

Fix saving fragments in TabsAdapter. Previously it didn't work when the activity was destroyed.