package com.martin.kantidroid.ui.overview;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.martin.kantidroid.R;
import com.martin.kantidroid.logic.PromoCheck;
import com.martin.kantidroid.logic.PromoRes;

import java.util.ArrayList;
import java.util.List;

public class OverviewFragment extends Fragment {

    private Adapter mAdapter;
    private TextView mPromo, mPP, mKont;
    private SharedPreferences mPrefs;
    private SharedPreferences.Editor mEditor;
    private ViewPager mViewPager;
    private boolean mFirsttime = false;

    public static OverviewFragment newInstance() {
        return new OverviewFragment();
    }

    public OverviewFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.overview_fragment, container, false);

        Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

        final ActionBar ab = ((AppCompatActivity) getActivity()).getSupportActionBar();
        ab.setHomeAsUpIndicator(R.drawable.ic_menu);
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setTitle(R.string.overview);

        mPromo = (TextView) rootView.findViewById(R.id.tvMadeIt);
        mPP = (TextView) rootView.findViewById(R.id.tvPP);
        mKont = (TextView) rootView.findViewById(R.id.tvKont);

        mPrefs = getActivity().getSharedPreferences("Kantidroid", Context.MODE_PRIVATE);
        mEditor = mPrefs.edit();

        mViewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
        if (mViewPager != null) {
            setupViewPager(mViewPager);
            mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }

                @Override
                public void onPageSelected(int position) {
                    showInfo(position + 1);
                    mEditor.putInt("semester", position);
                    mEditor.commit();
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }

        TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

        // TODO: Display last used tab & show according info
        mViewPager.setCurrentItem(mPrefs.getInt("semester", 0));
        showInfo(mPrefs.getInt("semester", 0) + 1);

        mFirsttime = true;

        return rootView;
    }

    public void loadData() {
        mAdapter.loadData();
        showInfo(mViewPager.getCurrentItem() + 1);
    }

    private void showInfo(int semester) {
        PromoRes promo = new PromoCheck(getActivity()).getGym(semester);
        mPromo.setText(promo.sMessage);
        mPromo.setTextColor(getResources().getColor(promo.iColor));
        mPP.setText(promo.sPP);
        mKont.setText(promo.sKont);
    }

    private void setupViewPager(ViewPager viewPager) {
        mAdapter = new Adapter(getChildFragmentManager());
        mAdapter.addFragment(OverviewSubjectsFragment.newInstance(1), getString(R.string.first_semester));
        mAdapter.addFragment(OverviewSubjectsFragment.newInstance(2), getString(R.string.second_semester));
        viewPager.setAdapter(mAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mFirsttime) {
            mFirsttime = false;
        } else {
            mAdapter.loadData();
            showInfo(mViewPager.getCurrentItem() + 1);
        }
    }

    static class Adapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitles = new ArrayList<>();

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }

        // TODO: Maybe do this off-thread?
        public void loadData() {
            for (int i = 0; i < mFragments.size(); i++) {
                ((OverviewSubjectsFragment) mFragments.get(i)).loadData();
            }
        }
    }
}

@@ -2756,16 +2756,22 @@
 elected(
+final 
 int posi
@@ -2798,36 +2798,439 @@
     
-    showInfo(position + 1);%0A
+%7D%0A%0A                @Override%0A                public void onPageScrollStateChanged(int state) %7B%0A                    if (state == ViewPager.SCROLL_STATE_IDLE) %7B%0A                        final int position = mViewPager.getCurrentItem();%0A                        showInfo(position + 1);%0A                        new Thread(new Runnable() %7B%0A                            @Override%0A                            public void run() %7B%0A            
     
@@ -3295,32 +3295,44 @@
                 
+       
+     
 mEditor.commit()
@@ -3337,39 +3337,32 @@
 ();%0A            
-    %7D%0A%0A
                 
@@ -3357,34 +3357,31 @@
             
-@Override%0A
+%7D%0A     
             
@@ -3388,57 +3388,44 @@
     
-public void onPageScrollStateChanged(int state) %7B
+   %7D).start();%0A                    %7D
 %0A%0A  
@@ -3975,16 +3975,22 @@
 howInfo(
+final 
 int seme
@@ -4009,73 +4009,324 @@
     
-PromoRes promo = new PromoCheck(getActivity()).getGym(semester);%0A
+new Thread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                final PromoRes promo = new PromoCheck(getActivity()).getGym(semester);%0A                getActivity().runOnUiThread(new Runnable() %7B%0A                    @Override%0A                    public void run() %7B%0A                
     
@@ -4357,24 +4357,40 @@
 .sMessage);%0A
+                
         mPro
@@ -4441,24 +4441,40 @@
 o.iColor));%0A
+                
         mPP.
@@ -4489,24 +4489,40 @@
 promo.sPP);%0A
+                
         mKon
@@ -4541,24 +4541,100 @@
 omo.sKont);%0A
+                    %7D%0A                %7D);%0A            %7D%0A        %7D).start();%0A
     %7D%0A%0A    p

Optimize all the things