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 android.widget.TextView;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseActivity;
import com.qhclh.ytzh.home.RpcUrl;
import com.qhclh.ytzh.tasks.ViewOnClickTask;
import com.qhclh.ytzh.ui.CalendarDialogFragment;
import com.qhclh.ytzh.utils.DateTimeUtil;
import com.qhclh.ytzh.work.tablepoultry.changle.ChangleFragment;
import org.forks.jsonrpc.JsonRpcResult;
import org.forks.jsonrpc.RpcFacade;
import org.forks.jsonrpc.RpcObject;
import org.greenrobot.eventbus.EventBus;

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

import butterknife.BindView;

/**
 * Created by 青花瓷 on 2017/12/4.
 */
///< 商品鸡报表
public class TablepoultryActivity extends BaseActivity implements CalendarDialogFragment.CalendarClickListener{
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tabLayout_tablep)
    TabLayout mTabLayout;
    @BindView(R.id.viewPager_tablep)
    ViewPager mViewPager;
    @BindView(R.id.tv_toolbar_title)
    TextView tv_toolbar_title;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private List<String> mTitleList = new ArrayList<>();
    private TabViewPagerAdapter mViewPagerAdapter;

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

    @Override
    protected void initView() {
        initToolbar(mToolbar, "", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tv_toolbar_title.setText("商品鸡报表");
    }

    @Override
    protected void initData() {
        areasList = new ArrayList<>();
        itemIdList = new ArrayList<>();
    }

    @Override
    protected void initOper() {

    }

    private boolean flag=false;
    @Override
    protected void onResume() {
        super.onResume();
        if (flag==false) {
            GetAllWeatherAreas(System.currentTimeMillis()- 1000 * 60 * 60 * 24);
            tv_toolbar_title.setText(DateTimeUtil.getyestoday());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        flag= false;
    }

    private List<RpcObject> rpcList;
    private List<Long> itemIdList;
    private void GetAllWeatherAreas(final long time) {
        ViewOnClickTask GetAllWeatherAreasTask = new ViewOnClickTask(this,"加载中...") {

            @Override
            protected void successUI() {
                areasList.clear();
                itemIdList.clear();
                mTitleList.clear();
                mFragmentList.clear();
                for (int i=0;i<rpcList.size();i++){
                    areasList.add(rpcList.get(i).getString("Name"));
                    itemIdList.add(rpcList.get(i).getLong("ID"));
                }
                initTabLayout();
                initViewPager(time);
            }

            @Override
            public Object call() throws Exception {
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.GetAllWeatherAreas);
                rpcList = result.getRpcObjectList(RpcUrl.GetAllWeatherAreas_fanhui);
                return null;
            }
        };
        GetAllWeatherAreasTask.execute();
    }

    private List<String> areasList;
    private void initViewPager(long time) {
        for (int i=0;i<areasList.size();i++) {
            mFragmentList.add( ChangleFragment.newInstance(itemIdList.get(i),time));
//            System.out.println("aaa++++++++mFragmentList+"+mFragmentList.get(i).getArguments().getLong("ARG_TIMELINE_TYPE"));
//            if (mFragmentList.get(i).getArguments().getLong("ARG_TIMELINE_TYPE")==2){
//               System.out.println("aaa++++id+++"+ i);
//            }
        }
        mViewPagerAdapter = new TabViewPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mViewPagerAdapter);
        mViewPager.setOffscreenPageLimit(1);
        mTabLayout.setupWithViewPager(mViewPager);
    }


    private void initTabLayout() {
        for (int i=0;i<rpcList.size();i++) {
            mTitleList.add(rpcList.get(i).getString("Name"));
        }

        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }

    @Override
    public void onStartCalendarClick(Date date) {
        GetAllWeatherAreas(date.getTime());
        flag = true;
        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);
        }
    }

    @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:
                new CalendarDialogFragment().show(getSupportFragmentManager(), "start");
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
