package com.qhclh.ytzh.work.tablepoultry;

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 com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseActivity;
import com.qhclh.ytzh.work.tablepoultry.anqiu.AnqiuFragment;
import com.qhclh.ytzh.work.tablepoultry.changle.ChangleFragment;
import com.qhclh.ytzh.work.tablepoultry.qiuping.QiupingFragment;
import com.qhclh.ytzh.work.tablepoultry.shouguangdong.ShouguangdFragment;
import com.qhclh.ytzh.work.tablepoultry.shouguangxi.ShouguangxFragment;

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

import butterknife.BindView;

/**
 * Created by 青花瓷 on 2017/12/4.
 */
///< 肉禽
public class TablepoultryActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tabLayout_tablep)
    TabLayout mTabLayout;
    @BindView(R.id.viewPager_tablep)
    ViewPager mViewPager;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private List<String> mTitleList = new ArrayList<>();
    private TabViewPagerAdapter mViewPagerAdapter;

    private int x=0;
    @Override
    protected int setLayoutId() {
        return R.layout.act_tablepoultry;
    }

    @Override
    protected void initView() {
//        x=1;
        initToolbar(mToolbar, "肉禽", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        initTabLayout();
        initViewPager();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initOper() {

    }

    private void initViewPager() {
        if (x==0) {
            mFragmentList.add(new ChangleFragment());
            mFragmentList.add(new ShouguangdFragment());
            mFragmentList.add(new ShouguangxFragment());
            mFragmentList.add(new AnqiuFragment());
            mFragmentList.add(new QiupingFragment());
        }else if (x==1){
            mFragmentList.add(new ShouguangxFragment());
            mFragmentList.add(new AnqiuFragment());
        }
        mViewPagerAdapter = new TabViewPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mViewPagerAdapter);
        mViewPager.setOffscreenPageLimit(2);
        mTabLayout.setupWithViewPager(mViewPager);
    }


    private void initTabLayout() {
        if (x==0) {
            mTitleList.add(getString(R.string.changle));
            mTitleList.add(getString(R.string.shouguangdong));
            mTitleList.add(getString(R.string.shouguangxi));
            mTitleList.add(getString(R.string.anqiu));
            mTitleList.add(getString(R.string.qiuping));
        }else if (x==1){
            mTitleList.add(getString(R.string.shouguangxi));
            mTitleList.add(getString(R.string.anqiu));
        }

//        for (String title : mTitleList) {
//            mTabLayout.addTab(mTabLayout.newTab().setText(title));
//        }

        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
    }

    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);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search, menu);
        menu.findItem(R.id.search_addr).setVisible(true);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.search_addr:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
