package com.bixolon.sample;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.SparseArray;
import android.view.ViewGroup;

public class TabPagerAdapter extends FragmentStatePagerAdapter {
    private Context mContext;
    SparseArray<Fragment> registeredFragments = new SparseArray<>();

    private PosFragment posFragment = null;
    private LabelFragment labelFragment = null;
    private HIDFragment hidFragment = null;
    private MSRFragment msrFragment = null;
    private ScannerFragment scannerFragment = null;
    private RFIDFragment rfidFragment = null;
    private DallasKeyFragment dallasKeyFragment = null;
    private UnknownUSBFragment unknownUSBFragment = null;
    private DisplayFragment displayFragment = null;
    private USBToSerialFragment usbToSerialFragment = null;
    private ScaleFragment scaleFragment = null;
    private ConfigFragment configFragment = null;

    private int[] mTabTitle = {R.string.POS, R.string.LABEL, R.string.HID, R.string.MSR, R.string.SCANNER, R.string.RFID, R.string.DALLASKEY, R.string.UNUSB, R.string.BCD, R.string.USBSERIAL, R.string.SCALE, R.string.CONFIG};

    public TabPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.mContext = context;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        registeredFragments.put(position, fragment);
        return fragment;
    }

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

    public Fragment getRegisteredFragment(int position) {
        return registeredFragments.get(position);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                if(posFragment == null) {
                    posFragment = PosFragment.newInstance();
                }
                return posFragment;
            case 1:
                if(labelFragment == null) {
                    labelFragment = LabelFragment.newInstance();
                }
                return labelFragment;
            case 2:
                if(hidFragment == null) {
                    hidFragment = HIDFragment.newInstance();
                }
                return hidFragment;
            case 3:
                if(msrFragment == null) {
                    msrFragment = MSRFragment.newInstance();
                }
                return msrFragment;
            case 4:
                if(scannerFragment == null) {
                    scannerFragment = ScannerFragment.newInstance();
                }
                return scannerFragment;
            case 5:
                if(rfidFragment == null) {
                    rfidFragment = RFIDFragment.newInstance();
                }
                return rfidFragment;
            case 6:
                if(dallasKeyFragment == null) {
                    dallasKeyFragment = DallasKeyFragment.newInstance();
                }
                return dallasKeyFragment;
            case 7:
                if(unknownUSBFragment == null) {
                    unknownUSBFragment = UnknownUSBFragment.newInstance();
                }
                return unknownUSBFragment;
            case 8:
                if(displayFragment == null) {
                    displayFragment = DisplayFragment.newInstance();
                }
                return displayFragment;
            case 9:
                if(usbToSerialFragment == null) {
                    usbToSerialFragment = USBToSerialFragment.newInstance();
                }
                return usbToSerialFragment;
            case 10:
                if(scaleFragment == null) {
                    scaleFragment = ScaleFragment.newInstance();
                }
                return scaleFragment;
            case 11:
                if(configFragment == null) {
                    configFragment = ConfigFragment.newInstance();
                }
                return configFragment;

            default:
                return null;
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mContext.getString(mTabTitle[position]);
    }

    @Override
    public int getCount() {
        return mTabTitle.length;
    }
}
