package com.qhclh.ytzh.work.Poultrydailyreport;

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.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseActivity;
import com.qhclh.ytzh.ui.CalendarDialogFragment;
import com.qhclh.ytzh.utils.DateTimeUtil;
import com.qhclh.ytzh.work.tablepoultry.ChoseEvent;

import org.greenrobot.eventbus.EventBus;

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

import butterknife.BindView;

/**
 * Created by 青花瓷 on 2018/3/22.
 */
///< 种禽日报表
public class PoultryDailyReportActivity extends BaseActivity implements CalendarDialogFragment.CalendarClickListener{
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_toolbar_title)
    TextView tv_toolbar_title;
    @BindView(R.id.tabLayout_pdr)
    TabLayout mTabLayout;
    @BindView(R.id.viewPager_pdr)
    ViewPager mViewPager;

    private List<Fragment> mFragmentList = new ArrayList<>();
    private List<String> mTitleList = new ArrayList<>();
    private TabViewPagerAdapter mViewPagerAdapter;

    @Override
    protected int setLayoutId() {
        return R.layout.act_poultrydailyreport;
    }

    @Override
    protected void initView() {
        initToolbar(mToolbar, "", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tv_toolbar_title.setText(DateTimeUtil.getYMD2(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24)));
        initTabLayout();
        initViewPager();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initOper() {

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.chose_time:
                new CalendarDialogFragment().show(getSupportFragmentManager(), "start");
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    private void initTabLayout() {
        mTitleList.add("育雏育成期");
        mTitleList.add("产蛋期");
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
    }

    private void initViewPager() {
        mFragmentList.add(new YuchuFragment());
        mFragmentList.add(new ChandanFragment());

        mViewPagerAdapter = new TabViewPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mViewPagerAdapter);
        mViewPager.setOffscreenPageLimit(1);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    public void onStartCalendarClick(Date date) {
        tv_toolbar_title.setText(DateTimeUtil.getYMD2(date));
        EventBus.getDefault().postSticky(new ChoseEvent(date.getTime()));
    }

    @Override
    public void onEndCalendarClick(Date date) {

    }

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

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

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


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