package com.qhclh.ytzh.work.productiondaily;

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

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseActivity;
import com.qhclh.ytzh.bean.WriteBean;
import com.qhclh.ytzh.home.RpcUrl;
import com.qhclh.ytzh.tasks.ViewOnClickTask;
import com.qhclh.ytzh.utils.DateTimeUtil;

import org.forks.jsonrpc.JsonRpcResult;
import org.forks.jsonrpc.RpcFacade;
import org.forks.jsonrpc.RpcObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by 青花瓷 on 2017/12/20.
 */

public class ShowInfoActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_toolbar_title)
    TextView tv_toolbar_title;
    @BindView(R.id.tabLayout_dailyinfo)
    TabLayout mTabLayout;
    @BindView(R.id.viewPager_dailyinfo)
    ViewPager mViewPager;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private List<String> mTitleList = new ArrayList<>();
    private TabViewPagerAdapter mViewPagerAdapter;

    private List<RpcObject> rpcObjectList;
    public static List<WriteBean> list=null;

    private long id;
    @Override
    protected int setLayoutId() {
        return R.layout.act_dailyinfo;
    }

    @Override
    protected void initView() {
        initToolbar(mToolbar, "", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tv_toolbar_title.setText("生产日报详情");

    }

    @Override
    protected void initData() {
        id = getIntent().getLongExtra("id",0);
        list = new ArrayList<>();

    }

    @Override
    protected void initOper() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        LoadOne();
    }

    private void LoadOne() {
        ViewOnClickTask DayProductRpcLoadTask = new ViewOnClickTask(this,"加载中...") {
            RpcObject rpcobj;

            @Override
            protected void successUI() {
                list.clear();

                tv_toolbar_title.setText(DateTimeUtil.getYMD2(rpcobj.getDate("Date")));
                rpcObjectList = rpcobj.getManyList("Details");

                for (int i=0;i<rpcObjectList.size();i++){
                    WriteBean productionDailyDetailBean = new WriteBean();
                    if (rpcObjectList.get(i).getLong("House_ID")!=null) {
                        productionDailyDetailBean.setHouseid(rpcObjectList.get(i).getLong("House_ID"));
                    }else {
                        productionDailyDetailBean.setHouseid(-1000);
                    }
                    if (rpcObjectList.get(i).getString("House_Name")!=null){
                        productionDailyDetailBean.setHousename(rpcObjectList.get(i).getString("House_Name"));
                    }else {
                        productionDailyDetailBean.setHousename("");
                    }

                    if (rpcObjectList.get(i).getInt("DieNumber")!=null){
                        productionDailyDetailBean.setSiwang(rpcObjectList.get(i).getInt("DieNumber")+"");
                    }else {
                        productionDailyDetailBean.setSiwang("-1000");
                    }
                    if (rpcObjectList.get(i).getInt("Obsolete")!=null){
                        productionDailyDetailBean.setTaotai(rpcObjectList.get(i).getInt("Obsolete")+"");
                    }else {
                        productionDailyDetailBean.setTaotai("-1000");
                    }
                    if (rpcObjectList.get(i).getDecimal("WaterIntake")!=null){
                        productionDailyDetailBean.setYinshuiliang(rpcObjectList.get(i).getDecimal("WaterIntake")+"");
                    }else {
                        productionDailyDetailBean.setYinshuiliang(new BigDecimal(-1000)+"");
                    }
                    if (rpcObjectList.get(i).getDecimal("Weight")!=null){
                        productionDailyDetailBean.setTizhong(rpcObjectList.get(i).getDecimal("Weight")+"");
                    }else {
                        productionDailyDetailBean.setTizhong(new BigDecimal(-1000)+"");
                    }

                    if (rpcObjectList.get(i).getDecimal("MinTemperature")!=null){
                        productionDailyDetailBean.setZuidiwendu(rpcObjectList.get(i).getDecimal("MinTemperature")+"");
                    }else {
                        productionDailyDetailBean.setZuidiwendu("-1000");
                    }

                    if (rpcObjectList.get(i).getDecimal("MaxTemperature")!=null){
                        productionDailyDetailBean.setZuigaowendu(rpcObjectList.get(i).getDecimal("MaxTemperature")+"");
                    }else {
                        productionDailyDetailBean.setZuigaowendu("-1000");
                    }

                    if (rpcObjectList.get(i).getDecimal("MinHumidity")!=null){
                        productionDailyDetailBean.setZuidishidu(rpcObjectList.get(i).getDecimal("MinHumidity")+"");
                    }else {
                        productionDailyDetailBean.setZuidishidu("-1000");
                    }

                    if (rpcObjectList.get(i).getDecimal("MaxHumidity")!=null){
                        productionDailyDetailBean.setZuigaoshidu(rpcObjectList.get(i).getDecimal("MaxHumidity")+"");
                    }else {
                        productionDailyDetailBean.setZuigaoshidu("-1000");
                    }

                    if (rpcObjectList.get(i).getString("NightMinAirQuantity")!=null){
                        productionDailyDetailBean.setZuixiaotongfeng(rpcObjectList.get(i).getString("NightMinAirQuantity"));
                    }else {
                        productionDailyDetailBean.setZuixiaotongfeng("-1000");
                    }

                    if (rpcObjectList.get(i).getString("DayMaxAirQuantity")!=null){
                        productionDailyDetailBean.setZuidatongfeng(rpcObjectList.get(i).getString("DayMaxAirQuantity"));
                    }else {
                        productionDailyDetailBean.setZuidatongfeng("-1000");
                    }

                    if (rpcObjectList.get(i).getString("NegativePressure")!=null){
                        productionDailyDetailBean.setFuya(rpcObjectList.get(i).getString("NegativePressure")+"");
                    }else {
                        productionDailyDetailBean.setFuya("-1000");
                    }

                    list.add(productionDailyDetailBean);
                }
                initTabLayout();
                initViewPager();
            }

            @Override
            public Object call() throws Exception {

                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.DayProductRpc_Load,id);
                rpcobj = result.getRpcObject(RpcUrl.DayProductRpc_Query_fanhui);
                return null;
            }
        };
        DayProductRpcLoadTask.execute();
    }

    private void initTabLayout() {

        for (int i = 0; i < rpcObjectList.size(); i++) {
            mTitleList.add(rpcObjectList.get(i).getString("House_Name"));
        }
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (list!=null){
            list.clear();
            list =null;
        }
        if (mFragmentList!=null){
            mFragmentList.clear();
            mFragmentList =null;
        }
        if (mTitleList!=null){
            mTitleList.clear();
            mTitleList =null;
        }

    }

    private void initViewPager() {

        for (int i = 0; i < rpcObjectList.size(); i++) {
//            mFragmentList.add(new ShowInfoFragment(list));
            mFragmentList.add(new ShowInfoFragment());
        }


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


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