package com.satsumasoftware.timetable.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.satsumasoftware.timetable.R;
import com.satsumasoftware.timetable.TimetableApplication;
import com.satsumasoftware.timetable.db.util.ClassUtils;
import com.satsumasoftware.timetable.framework.Class;
import com.satsumasoftware.timetable.framework.ClassDetail;
import com.satsumasoftware.timetable.framework.ClassTime;
import com.satsumasoftware.timetable.framework.Timetable;
import com.satsumasoftware.timetable.ui.adapter.ScheduleAdapter;
import com.satsumasoftware.timetable.util.DateUtils;
import com.satsumasoftware.timetable.util.ThemeUtils;

import org.threeten.bp.DayOfWeek;
import org.threeten.bp.LocalDate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class ScheduleActivity extends BaseActivity {

    protected static final int REQUEST_CODE_CLASS_DETAIL = 1;

    private DynamicPagerAdapter mPagerAdapter;
    private ViewPager mViewPager;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

        mPagerAdapter = new DynamicPagerAdapter();

        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mViewPager.setAdapter(mPagerAdapter);

        setupLayout();

        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setTabTextColors(
                ContextCompat.getColor(this, R.color.mdu_text_white_secondary),
                ContextCompat.getColor(this, R.color.mdu_text_white));
        tabLayout.setupWithViewPager(mViewPager);

        goToNow();
    }

    private void setupLayout() {
        mPagerAdapter.removeAllViews(mViewPager);

        Timetable timetable = ((TimetableApplication) getApplication()).getCurrentTimetable();
        assert timetable != null;

        if (!timetable.isValidToday()) {
            View placeholder = ThemeUtils.makePlaceholderView(this,
                    R.drawable.ic_today_black_24dp, R.string.home_card_classes_placeholder);
            mPagerAdapter.addViewWithTitle(
                    placeholder, getString(R.string.title_activity_schedule));
            return;
        }

        for (int weekNumber = 1; weekNumber <= timetable.getWeekRotations(); weekNumber++) {
            for (DayOfWeek dayOfWeek : DayOfWeek.values()) {

                StringBuilder titleBuilder = new StringBuilder();
                titleBuilder.append(dayOfWeek.toString());
                if (!timetable.hasFixedScheduling()) {
                    titleBuilder.append(" ")
                            .append(ClassTime.getWeekText(this, weekNumber, false));
                }
                String tabTitle = titleBuilder.toString();

                final ArrayList<ClassTime> classTimes =
                        ClassUtils.getClassTimesForDay(this, dayOfWeek, weekNumber);

                if (classTimes.isEmpty()) {
                    View placeholder = ThemeUtils.makePlaceholderView(this,
                            R.drawable.ic_today_black_24dp, R.string.home_card_classes_placeholder);
                    mPagerAdapter.addViewWithTitle(placeholder, tabTitle);
                    continue;
                }

                Collections.sort(classTimes, new Comparator<ClassTime>() {
                    @Override
                    public int compare(ClassTime t1, ClassTime t2) {
                        int startTimeComparison = t1.getStartTime().compareTo(t2.getStartTime());
                        if (startTimeComparison == 0) {
                            return t1.getEndTime().compareTo(t2.getEndTime());
                        }
                        return startTimeComparison;
                    }
                });

                ScheduleAdapter adapter = new ScheduleAdapter(this, classTimes);
                adapter.setOnEntryClickListener(new ScheduleAdapter.OnEntryClickListener() {
                    @Override
                    public void onEntryClick(View view, int position) {
                        ClassTime classTime = classTimes.get(position);
                        ClassDetail classDetail = ClassDetail.create(
                                getBaseContext(), classTime.getClassDetailId());
                        Class cls = Class.create(getBaseContext(), classDetail.getClassId());

                        Intent intent = new Intent(ScheduleActivity.this, ClassEditActivity.class);
                        intent.putExtra(ClassEditActivity.EXTRA_CLASS, cls);
                        startActivityForResult(intent, REQUEST_CODE_CLASS_DETAIL);
                    }
                });

                RecyclerView recyclerView = new RecyclerView(this);
                recyclerView.setLayoutManager(new LinearLayoutManager(this));
                recyclerView.setHasFixedSize(true);
                recyclerView.addItemDecoration(new DividerItemDecoration(
                        this, DividerItemDecoration.VERTICAL_LIST));
                recyclerView.setAdapter(adapter);

                mPagerAdapter.addViewWithTitle(recyclerView, tabTitle);
            }
        }
    }

    private void goToNow() {
        DayOfWeek today = LocalDate.now().getDayOfWeek();
        int nthWeek = DateUtils.findWeekNumber(getApplication());

        int index = today.getValue() + ((nthWeek - 1) * 7) - 1;
        mViewPager.setCurrentItem(index);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE_CLASS_DETAIL) {
            if (resultCode == Activity.RESULT_OK) {
                setupLayout();
                goToNow();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_schedule, menu);
        ThemeUtils.tintMenuIcons(this, menu, R.id.action_today, R.id.action_view_week);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_today:
                goToNow();
                break;
            case R.id.action_view_week:
                Snackbar.make(findViewById(R.id.drawerLayout), "Week view coming soon",
                        Snackbar.LENGTH_SHORT).show();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected Toolbar getSelfToolbar() {
        return (Toolbar) findViewById(R.id.toolbar);
    }

    @Override
    protected DrawerLayout getSelfDrawerLayout() {
        return (DrawerLayout) findViewById(R.id.drawerLayout);
    }

    @Override
    protected int getSelfNavDrawerItem() {
        return NAVDRAWER_ITEM_SCHEDULE;
    }

    @Override
    protected NavigationView getSelfNavigationView() {
        return (NavigationView) findViewById(R.id.navigationView);
    }

}

@@ -262,32 +262,131 @@
 dget.TabLayout;%0A
+import android.support.v4.app.ActivityCompat;%0Aimport android.support.v4.app.ActivityOptionsCompat;%0A
 import android.s
@@ -5494,24 +5494,25 @@
 LASS, cls);%0A
+%0A
             
@@ -5527,31 +5527,628 @@
     
-startActivityForResult(
+Bundle bundle = null;%0A                        if (ThemeUtils.isApi21()) %7B%0A                            ActivityOptionsCompat options =%0A                                    ActivityOptionsCompat.makeSceneTransitionAnimation(%0A                                            ScheduleActivity.this,%0A                                            view,%0A                                            getString(R.string.transition_1));%0A                            bundle = options.toBundle();%0A                        %7D%0A%0A                        ActivityCompat.startActivityForResult(%0A                                ScheduleActivity.this, 
 inte
@@ -6176,16 +6176,24 @@
 S_DETAIL
+, bundle
 );%0A     

Add animations for the schedule page