package com.codepath.apps.mysimpletweets.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.codepath.apps.mysimpletweets.R;
import com.codepath.apps.mysimpletweets.adapters.TwitterFragmentPagerAdapter;
import com.codepath.apps.mysimpletweets.fragments.CreateTweetFragment;
import com.codepath.apps.mysimpletweets.fragments.HomeTimelineFragment;
import com.codepath.apps.mysimpletweets.fragments.UserTimelineFragment;
import com.codepath.apps.mysimpletweets.models.Tweet;

public class TimelineActivity extends AppCompatActivity implements CreateTweetFragment.TweetCreatedListener {

    private TwitterFragmentPagerAdapter mTwitterFragmentPagerAdapter;
    private ViewPager mViewPager;

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

        //mHomeTimeLIneFragment = (HomeTimelineFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_timeline);

        // Get the ViewPager and set it's PagerAdapter so that it can display items
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mTwitterFragmentPagerAdapter = new TwitterFragmentPagerAdapter(getSupportFragmentManager(),
                TimelineActivity.this);
        mViewPager.setAdapter(mTwitterFragmentPagerAdapter);

        // Give the TabLayout the ViewPager
        TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
        tabLayout.setupWithViewPager(mViewPager);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    private void showCreateTweetDialog() {
        FragmentManager fm = getSupportFragmentManager();
        ;
        CreateTweetFragment createTweetFragment = CreateTweetFragment.newInstance(
                "Compose a new tweet");
        createTweetFragment.show(fm, "fragment_create_tweet");
    }

    public void onComposeTweet(MenuItem item) {
        showCreateTweetDialog();
    }

    @Override
    public void onTweetCreated(Tweet tweet) {
        HomeTimelineFragment homeTimelineFragment =
                (HomeTimelineFragment) mTwitterFragmentPagerAdapter.getRegisteredFragment(TwitterFragmentPagerAdapter.HOME_TIMELINE_PAGE_NUMBER);
        homeTimelineFragment.addTweet(tweet);
        homeTimelineFragment.scrollToTop();
    }


    public void onProfileView(MenuItem mi) {
        Intent i = new Intent(this, ProfileActivity.class);
        String screenName = null;
        i.putExtra(UserTimelineFragment.SCREEN_NAME_KEY, screenName);
        startActivity(i);
    }

    public void switchTab(int tabNumber) {
        mViewPager.setCurrentItem(tabNumber);
    }
}
