/*
 * Copyright (C) 2015 Chobob City
 *
 * 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 kr.nogcha.sejongbus;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;

public class MainActivity extends ActionBarActivity {
    private static Activity mInstance;
    private ViewPager viewPager;

    public static void hideSoftInput() {
        final InputMethodManager inputMethodManager =
                (InputMethodManager) mInstance.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.toggleSoftInput(0, 0);
    }

    public static void startHostActivity(int... args) {
        Intent intent = new Intent(mInstance, TrafficActivity.class);
        for (int i = 0; i < args.length; i++) intent.putExtra("arg" + i, args[i]);
        mInstance.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_main);

        mInstance = this;

        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        viewPager = (ViewPager) findViewById(R.id.pager);
        MainPagerAdapter mainPagerAdapter = new MainPagerAdapter(getFragmentManager());
        viewPager.setAdapter(mainPagerAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        for (int i = 0; i < mainPagerAdapter.getCount(); i++) {
            actionBar.addTab(actionBar.newTab().setText(mainPagerAdapter.getPageTitle(i))
                    .setTabListener(new ActionBar.TabListener() {
                        @Override
                        public void onTabSelected(ActionBar.Tab tab,
                                                  FragmentTransaction fragmentTransaction) {
                            viewPager.setCurrentItem(tab.getPosition());
                        }

                        @Override
                        public void onTabUnselected(ActionBar.Tab tab,
                                                    FragmentTransaction fragmentTransaction) {
                        }

                        @Override
                        public void onTabReselected(ActionBar.Tab tab,
                                                    FragmentTransaction fragmentTransaction) {
                        }
                    }));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private class MainPagerAdapter extends FragmentPagerAdapter {
        public MainPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new MainFragment1();
                case 1:
                    return new MainFragment2();
                case 2:
                    return new MainFragment3();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "통합검색";
                case 1:
                    return "경로탐색";
                case 2:
                    return "주변정류소";
                default:
                    return super.getPageTitle(position);
            }
        }
    }
}

@@ -1267,20 +1267,17 @@
 ewPager 
-view
+m
 Pager;%0A%0A
@@ -1772,32 +1772,69 @@
 intent);%0A    %7D%0A%0A
+    @SuppressWarnings(%22deprecation%22)%0A
     @Override%0A  
@@ -2013,141 +2013,9 @@
     
-final ActionBar actionBar = getSupportActionBar();%0A        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);%0A%0A        view
+m
 Page
@@ -2068,29 +2068,24 @@
     Main
-Pager
 Adapter 
 mainPage
@@ -2076,26 +2076,17 @@
 Adapter 
-mainPagerA
+a
 dapter =
@@ -2090,29 +2090,24 @@
 r = new Main
-Pager
 Adapter(getF
@@ -2125,36 +2125,33 @@
 ger());%0A        
-view
+m
 Pager.setAdapter
@@ -2151,32 +2151,82 @@
 Adapter(
-mainPagerAdapter
+adapter);%0A        final ActionBar actionBar = getSupportActionBar(
 );%0A     
@@ -2220,36 +2220,33 @@
 nBar();%0A        
-view
+m
 Pager.setOnPageC
@@ -2739,24 +2739,93 @@
        %7D);%0A%0A
+        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);%0A
         for 
@@ -2840,26 +2840,17 @@
  0; i %3C 
-mainPagerA
+a
 dapter.g
@@ -2927,18 +2927,9 @@
 ext(
-mainPagerA
+a
 dapt
@@ -3238,20 +3238,17 @@
         
-view
+m
 Pager.se
@@ -4263,29 +4263,24 @@
 e class Main
-Pager
 Adapter exte
@@ -4325,21 +4325,16 @@
 lic Main
-Pager
 Adapter(

Patch #3