package org.stepic.droid.ui.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import org.stepic.droid.R;
import org.stepic.droid.analytic.Analytic;
import org.stepic.droid.base.FragmentBase;
import org.stepic.droid.base.MainApplication;
import org.stepic.droid.ui.NotificationCategory;
import org.stepic.droid.ui.activities.HasDrawer;

import butterknife.BindView;
import butterknife.ButterKnife;
import timber.log.Timber;

public class NotificationsFragment extends FragmentBase {

    private ActionBarDrawerToggle actionBarDrawerToggle;

    public static NotificationsFragment newInstance() {
        Bundle args = new Bundle();
        NotificationsFragment fragment = new NotificationsFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @BindView(R.id.toolbar_fragment)
    Toolbar toolbar;

    @BindView(R.id.notification_tabs)
    TabLayout tabLayout;

    @BindView(R.id.notification_viewpager)
    ViewPager viewPager;

    private HasDrawer hasDrawerHost;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof HasDrawer) {
            hasDrawerHost = (HasDrawer) context;
        }
    }

    @Override
    public void onDetach() {
        hasDrawerHost = null;
        super.onDetach();
    }

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

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_notifications, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initToolbar();
        initViewPager();
    }

    private void initViewPager() {
        viewPager.setAdapter(new NotificationPagerAdapter(getChildFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    public void onDestroyView() {
        destroyToolbar();
        super.onDestroyView();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.notification_center_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                analytic.reportEvent(Analytic.Interaction.CLICK_SETTINGS_FROM_NOTIFICATION);
                shell.getScreenProvider().showSettings(getActivity());
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initToolbar() {
        toolbar.setTitle(R.string.notification_title);
        if (hasDrawerHost != null) {
            actionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), hasDrawerHost.getDrawerLayout(), toolbar, R.string.drawer_open, R.string.drawer_closed);
            hasDrawerHost.getDrawerLayout().addDrawerListener(actionBarDrawerToggle);
            actionBarDrawerToggle.syncState();
        }
        ActionBar supportActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.hide();
        }
    }

    private void destroyToolbar() {
        if (actionBarDrawerToggle != null && hasDrawerHost != null) {
            hasDrawerHost.getDrawerLayout().removeDrawerListener(actionBarDrawerToggle);
        }
        ActionBar supportActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.show();
        }
    }


    static class NotificationPagerAdapter extends FragmentStatePagerAdapter {
        private final int numberOfCategories = NotificationCategory.values().length;

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

        @Override
        public Fragment getItem(int position) {
            Timber.d("getItem %d", position);
            return NotificationListFragment.newInstance(position);
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            Timber.d("getPageTitle %d", position);
            int resString = NotificationCategory.values()[position].getTitle();
            return MainApplication.getAppContext().getString(resString);
        }
    }

}

@@ -594,68 +594,8 @@
 er;%0A
-import android.view.Menu;%0Aimport android.view.MenuInflater;%0A
 impo
@@ -2058,41 +2058,8 @@
 e);%0A
-        setHasOptionsMenu(true);%0A
     
@@ -2869,105 +2869,447 @@
     
-@Override%0A    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) %7B%0A        inflate
+private void initToolbar() %7B%0A        toolbar.setTitle(R.string.notification_title);%0A        if (hasDrawerHost != null) %7B%0A            actionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), hasDrawerHost.getDrawerLayout(), toolbar, R.string.drawer_open, R.string.drawer_closed);%0A            hasDrawerHost.getDrawerLayout().addDrawerListener(actionBarDrawerToggle);%0A            actionBarDrawerToggle.syncState();%0A%0A            toolba
 r.in
@@ -3313,16 +3313,20 @@
 .inflate
+Menu
 (R.menu.
@@ -3353,24 +3353,110 @@
 menu
-, menu);%0A    %7D%0A%0A
+);%0A            toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() %7B%0A            
     
@@ -3457,32 +3457,44 @@
       @Override%0A
+            
     public boole
@@ -3498,35 +3498,29 @@
 olean on
-OptionsItemSelected
+MenuItemClick
 (MenuIte
@@ -3525,24 +3525,36 @@
 tem item) %7B%0A
+            
         swit
@@ -3589,16 +3589,28 @@
         
+           
+ 
 case R.i
@@ -3628,16 +3628,28 @@
 ttings:%0A
+            
         
@@ -3749,16 +3749,28 @@
         
+   
+         
 shell.ge
@@ -3836,31 +3836,12 @@
     
-return true;%0A
+ 
    
-     %7D%0A
     
@@ -3855,471 +3855,101 @@
 urn 
-super.onOptionsItemSelected(item)
+true
 ;%0A    
-%7D%0A%0A
     
-private void initToolbar() %7B%0A        toolbar.setTitle(R.string.notification_title);%0A        if (hasDrawerHost != null) %7B%0A            actionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), hasDrawerHost.getDrawerLayout(), toolbar, R.string.drawer_open, R.string.drawer_closed);%0A            hasDrawerHost.getDrawerLayout().addDrawerListener(actionBarDrawerToggle);%0A            actionBarDrawerToggle.syncState(
+            %7D%0A                    return false;%0A                %7D%0A            %7D
 );%0A 
@@ -4344,32 +4344,82 @@
 gle);%0A        %7D%0A
+        toolbar.setOnMenuItemClickListener(null);%0A
         ActionBa

add settings icon to notifications