package com.qhclh.ytzh.work.productiondailyw;

import android.content.Intent;
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.bean.WriteBean;
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.productiondaily.ProductiondailyActivity;

import org.forks.jsonrpc.JsonRpcResult;
import org.forks.jsonrpc.RpcFacade;
import org.forks.jsonrpc.RpcObject;
import org.litepal.crud.DataSupport;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;

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

public class WriteDActivity 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_write)
    TabLayout mTabLayout;
    @BindView(R.id.viewPager_write)
    ViewPager mViewPager;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private List<String> mTitleList = new ArrayList<>();
    private TabViewPagerAdapter mViewPagerAdapter;

    public static List<WriteBean> list;
    private List<WriteBean> writeBeanList;

    public static List<WriteBean> tijiaoList;


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

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

    }

    @Override
    protected void initData() {
        list = new ArrayList<>();
        tijiaoList = new ArrayList<>();
    }

    @Override
    protected void initOper() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        tv_toolbar_title.setText(DateTimeUtil.getYMD2(new Date(System.currentTimeMillis()-1000*60*60*24)));
        try {
            LoadOrCreateEditingDayProduct(new SimpleDateFormat("yyyy-MM-dd").parse(DateTimeUtil.getYMD(new Date(System.currentTimeMillis()-1000*60*60*24))));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public static RpcObject rpcObj;

    private List<RpcObject> detailsList;
    private String houseid;
    private String housename;
    private String days;
    private String dienumber;
    private String obs;
    private String water;
    private String weight;
    private String zuidiwendu;
    private String zuigaowendu;
    private String zuidishidu;
    private String zuigaoshidu;
    private String zuixiaotongfeng;
    private String zuidatongfeng;
    private String fuya;

    private void LoadOrCreateEditingDayProduct(final Date time) {
        ViewOnClickTask LoadOrCreateEditingDayProductTask = new ViewOnClickTask(this, "加载中...") {
            @Override
            protected void successUI() {
                list.clear();
                tijiaoList.clear();
                mTitleList.clear();
                mFragmentList.clear();
                DataSupport.deleteAll(WriteBean.class);
                detailsList = rpcObj.getManyList("Details");
                for (int i = 0; i < detailsList.size(); i++) {
                    if (detailsList.get(i).getLong("House_ID") != null) {
                        houseid = detailsList.get(i).getLong("House_ID") + "";
                    } else {
                        houseid = "";
                    }

                    if (detailsList.get(i).getString("House_Name") != null) {
                        housename = detailsList.get(i).getString("House_Name");
                    } else {
                        housename = "";
                    }

                    if (detailsList.get(i).getInt("Days") != null) {
                        days = detailsList.get(i).getInt("Days") + "";
                    } else {
                        days = "";
                    }

                    if (detailsList.get(i).getInt("DieNumber") != null) {
                        dienumber = detailsList.get(i).getInt("DieNumber") + "";
                    } else {
                        dienumber = "";
                    }

                    if (detailsList.get(i).getInt("Obsolete") != null) {
                        obs = detailsList.get(i).getInt("Obsolete") + "";
                    } else {
                        obs = "";
                    }

                    if (detailsList.get(i).getDecimal("WaterIntake") != null) {
                        water = detailsList.get(i).getDecimal("WaterIntake") + "";
                    } else {
                        water = "";
                    }

                    if (detailsList.get(i).getDecimal("Weight") != null) {
                        weight = detailsList.get(i).getDecimal("Weight") + "";
                    } else {
                        weight = "";
                    }

                    if (detailsList.get(i).getDecimal("MinTemperature") != null) {
                        zuidiwendu = detailsList.get(i).getDecimal("MinTemperature") + "";
                    } else {
                        zuidiwendu = "";
                    }
                    if (detailsList.get(i).getDecimal("MaxTemperature") != null) {
                        zuigaowendu = detailsList.get(i).getDecimal("MaxTemperature") + "";
                    } else {
                        zuigaowendu = "";
                    }
                    if (detailsList.get(i).getDecimal("MinHumidity") != null) {
                        zuidishidu = detailsList.get(i).getDecimal("MinHumidity") + "";
                    } else {
                        zuidishidu = "";
                    }

                    if (detailsList.get(i).getDecimal("MaxHumidity") != null) {
                        zuigaoshidu = detailsList.get(i).getDecimal("MaxHumidity") + "";
                    } else {
                        zuigaoshidu = "";
                    }
                    if (detailsList.get(i).getString("NightMinAirQuantity") != null) {
                        zuixiaotongfeng = detailsList.get(i).getString("NightMinAirQuantity");
                    } else {
                        zuixiaotongfeng = "";
                    }
                    if (detailsList.get(i).getString("DayMaxAirQuantity") != null) {
                        zuidatongfeng = detailsList.get(i).getString("DayMaxAirQuantity");
                    } else {
                        zuidatongfeng = "";
                    }
                    if (detailsList.get(i).getString("NegativePressure") != null) {
                        fuya = detailsList.get(i).getString("NegativePressure") + "";
                    } else {
                        fuya = "";
                    }


                    WriteBean writebean = new WriteBean();
                    writebean.setHouseid(Long.parseLong(houseid));
                    writebean.setHousename(housename);
                    writebean.setSiwang(dienumber);
                    writebean.setTaotai(obs);
                    writebean.setYinshuiliang(water);
                    writebean.setTizhong(weight);
                    writebean.setZuidiwendu(zuidiwendu);
                    writebean.setZuigaowendu(zuigaowendu);
                    writebean.setZuidishidu(zuidishidu);
                    writebean.setZuigaoshidu(zuigaoshidu);
                    writebean.setZuixiaotongfeng(zuixiaotongfeng);
                    writebean.setZuidatongfeng(zuidatongfeng);
                    writebean.setFuya(fuya);
                    list.add(writebean);

                    WriteBean writeBeandb = new WriteBean();
                    writeBeandb.setHouseid(Long.parseLong(houseid));
                    writeBeandb.setHousename(housename);
                    writeBeandb.setSiwang(dienumber);
                    writeBeandb.setTaotai(obs);
                    writeBeandb.setYinshuiliang(water);
                    writeBeandb.setTizhong(weight);
                    writeBeandb.setZuidiwendu(zuidiwendu);
                    writeBeandb.setZuigaowendu(zuigaowendu);
                    writeBeandb.setZuidishidu(zuidishidu);
                    writeBeandb.setZuigaoshidu(zuigaoshidu);
                    writeBeandb.setZuixiaotongfeng(zuixiaotongfeng);
                    writeBeandb.setZuidatongfeng(zuidatongfeng);
                    writeBeandb.setFuya(fuya);
                    writeBeandb.save();

                }
                writeBeanList = DataSupport.findAll(WriteBean.class);
                initTabLayout();
                initViewPager();
            }

            @Override
            public Object call() throws Exception {
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.LoadOrCreateEditingDayProduct, time);
                rpcObj = result.getRpcObject(RpcUrl.LoadOrCreateEditingDayProduct_fanhui);
                return null;
            }
        };
        LoadOrCreateEditingDayProductTask.execute();

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.chose_time1:
                new CalendarDialogFragment().show(getSupportFragmentManager(), "start");
                break;
            case R.id.show_list:
                startActivity(new Intent(WriteDActivity.this, ProductiondailyActivity.class));
                break;
        }
        return super.onOptionsItemSelected(item);
    }



    private void initTabLayout() {

        for (int i = 0; i < writeBeanList.size(); i++) {
            mTitleList.add(writeBeanList.get(i).getHousename());
        }
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }

    private void initViewPager() {
        for (int i = 0; i < writeBeanList.size(); i++) {
//            mFragmentList.add(new WriteDFragment(list));
            mFragmentList.add(new WriteDFragment());
        }


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

    @Override
    public void onStartCalendarClick(Date date) {
        LoadOrCreateEditingDayProduct(date);
        tv_toolbar_title.setText(DateTimeUtil.getYMD2(date));
    }

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