package com.qhclh.ytzh.work.productiondailyw;

import android.content.ContentValues;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseFragment;
import com.qhclh.ytzh.bean.WriteBean;
import com.qhclh.ytzh.home.RpcUrl;
import com.qhclh.ytzh.tasks.ViewOnClickTask;

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

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

import butterknife.BindView;

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

public class WriteDFragment extends BaseFragment {
    @BindView(R.id.frag_death)
    EditText frag_death;
    @BindView(R.id.frag_eliminate)
    EditText frag_eliminate;
    @BindView(R.id.frag_water)
    EditText frag_water;
    @BindView(R.id.frag_weight)
    EditText frag_weight;
    @BindView(R.id.frag_mint)
    EditText frag_mint;
    @BindView(R.id.frag_maxt)
    EditText frag_maxt;
    @BindView(R.id.frag_minh)
    EditText frag_minh;
    @BindView(R.id.frag_maxh)
    EditText frag_maxh;
    @BindView(R.id.frag_minw)
    EditText frag_minw;
    @BindView(R.id.frag_maxw)
    EditText frag_maxw;
    @BindView(R.id.frag_pressure)
    EditText frag_pressure;
    @BindView(R.id.frag_tijiao)
    Button frag_tijiao;
    @BindView(R.id.frag_tijiao_zuihou)
    Button frag_tijiao_zuihou;

//    private List<WriteBean> list;
    private List<WriteBean> mWrite;

//    public WriteDFragment(List<WriteBean> list) {
//        this.list = list;
//    }

    private WriteDActivity activity;
    private String siwang, taotai, yinshuiliang, tizhong, zuidiwendu, zuigaowendu, zuidishidu, zuigaoshidu, zuixiaotongfeng, zuigaotongfeng, fuya;

    @Override
    protected int setLayout() {
        return R.layout.frag_daily;
    }

    @Override
    protected void initView() {
        activity = (WriteDActivity) getActivity();
        isViewCreated = true;
    }

    @Override
    protected void initData() {

    }

    //Fragment的View加载完毕的标记
    private boolean isViewCreated;

    //Fragment对用户可见的标记
    private boolean isUIVisible;
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            isUIVisible = true;
            lazyLoad();
        } else {
            isUIVisible = false;
        }

    }

    private void lazyLoad() {
        //这里进行双重标记判断,是因为setUserVisibleHint会多次回调,并且会在onCreateView执行前回调,必须确保onCreateView加载完毕且页面可见,才加载数据
        if (isViewCreated && isUIVisible) {
            List<WriteBean> dewrb =  DataSupport.findAll(WriteBean.class);

            frag_death.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getSiwang());
            frag_eliminate.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getTaotai());
            frag_water.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getYinshuiliang());
            frag_weight.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getTizhong());


            if ("".equals(dewrb.get(activity.mViewPager.getCurrentItem()).getZuidiwendu()) || dewrb.get(activity.mViewPager.getCurrentItem()).getZuidiwendu()==null){
                frag_mint.setText(dewrb.get(0).getZuidiwendu());
            }else {
                frag_mint.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getZuidiwendu());
            }

            if ("".equals(dewrb.get(activity.mViewPager.getCurrentItem()).getZuigaowendu()) || dewrb.get(activity.mViewPager.getCurrentItem()).getZuigaowendu()==null){
                frag_maxt.setText(dewrb.get(0).getZuigaowendu());
            }else {
                frag_maxt.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getZuigaowendu());
            }

            if ("".equals(dewrb.get(activity.mViewPager.getCurrentItem()).getZuidishidu()) || dewrb.get(activity.mViewPager.getCurrentItem()).getZuidishidu()==null){
                frag_minh.setText(dewrb.get(0).getZuidishidu());
            }else {
                frag_minh.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getZuidishidu());
            }

            if ("".equals(dewrb.get(activity.mViewPager.getCurrentItem()).getZuigaoshidu()) || dewrb.get(activity.mViewPager.getCurrentItem()).getZuigaoshidu()==null){
                frag_maxh.setText(dewrb.get(0).getZuigaoshidu());
            }else {
                frag_maxh.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getZuigaoshidu());
            }

            if ("".equals(dewrb.get(activity.mViewPager.getCurrentItem()).getZuixiaotongfeng()) || dewrb.get(activity.mViewPager.getCurrentItem()).getZuixiaotongfeng()==null){
                frag_minw.setText(dewrb.get(0).getZuixiaotongfeng());
            }else {
                frag_minw.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getZuixiaotongfeng());
            }

            if ("".equals(dewrb.get(activity.mViewPager.getCurrentItem()).getZuidatongfeng()) || dewrb.get(activity.mViewPager.getCurrentItem()).getZuidatongfeng()==null){
                frag_maxw.setText(dewrb.get(0).getZuidatongfeng());
            }else {
                frag_maxw.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getZuidatongfeng());
            }
            if ("".equals(dewrb.get(activity.mViewPager.getCurrentItem()).getFuya()) || dewrb.get(activity.mViewPager.getCurrentItem()).getFuya()==null){
                frag_pressure.setText(dewrb.get(0).getFuya());
            }else {
                frag_pressure.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getFuya());
            }

            if (activity.mViewPager.getCurrentItem() == (activity.mViewPager.getAdapter().getCount() - 1)) {
                frag_tijiao_zuihou.setVisibility(View.VISIBLE);
            }
            //数据加载完毕,恢复标记,防止重复加载
            isViewCreated = false;
            isUIVisible = false;

        }
    }

    @Override
    protected void initOper() {
        frag_tijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                siwang = frag_death.getText().toString().trim();
                taotai = frag_eliminate.getText().toString().trim();
                yinshuiliang = frag_water.getText().toString().trim();
                tizhong = frag_weight.getText().toString().trim();
                zuidiwendu = frag_mint.getText().toString().trim();
                zuigaowendu = frag_maxt.getText().toString().trim();
                zuidishidu = frag_minh.getText().toString().trim();
                zuigaoshidu = frag_maxh.getText().toString().trim();
                zuixiaotongfeng = frag_minw.getText().toString().trim();
                zuigaotongfeng = frag_maxw.getText().toString().trim();
                fuya = frag_pressure.getText().toString().trim();

                if (siwang.equals("") || siwang == null) {
                    showToast("请输入死亡只数");
                    return;
                }
                if (taotai.equals("") || taotai == null) {
                    showToast("请输入淘汰只数");
                    return;
                }
                if (yinshuiliang.equals("") || yinshuiliang == null) {
                    showToast("请输入饮水量");
                    return;
                }
                if (tizhong.equals("") || tizhong == null) {
                    showToast("请输入体重");
                    return;
                }
                if (zuidiwendu.equals("") || zuidiwendu == null) {
                    showToast("请输入最低温度");
                    return;
                }
                if (zuigaowendu.equals("") || zuigaowendu == null) {
                    showToast("请输入最高温度");
                    return;
                }
                if (zuidishidu.equals("") || zuidishidu == null) {
                    showToast("请输入最低湿度");
                    return;
                }
                if (zuigaoshidu.equals("") || zuigaoshidu == null) {
                    showToast("请输入最高湿度");
                    return;
                }
                if (zuixiaotongfeng.equals("") || zuixiaotongfeng == null) {
                    showToast("请输入最小通风");
                    return;
                }
                if (zuigaotongfeng.equals("") || zuigaotongfeng == null) {
                    showToast("请输入最大通风");
                    return;
                }
                if (fuya.equals("") || fuya == null) {
                    showToast("请输入负压");
                    return;
                }


//                if (activity.mViewPager.getCurrentItem() < (activity.mViewPager.getAdapter().getCount() - 1)) {
                    WriteBean writeBean = new WriteBean();
                    writeBean.setHouseid(WriteDActivity.list.get(activity.mViewPager.getCurrentItem()).getHouseid());
                    writeBean.setSiwang(siwang);
                    writeBean.setTaotai(taotai);
                    writeBean.setYinshuiliang(yinshuiliang);
                    writeBean.setTizhong(tizhong);
                    writeBean.setZuidiwendu(zuidiwendu);
                    writeBean.setZuigaowendu(zuigaowendu);
                    writeBean.setZuidishidu(zuidishidu);
                    writeBean.setZuigaoshidu(zuigaoshidu);
                    writeBean.setZuixiaotongfeng(zuixiaotongfeng);
                    writeBean.setZuidatongfeng(zuigaotongfeng);
                    writeBean.setFuya(fuya);
                    WriteDActivity.tijiaoList.add(writeBean);
                    ContentValues values = new ContentValues();
                    values.put("siwang", siwang);
                    values.put("taotai", taotai);
                    values.put("yinshuiliang", yinshuiliang);
                    values.put("tizhong", tizhong);
                    values.put("zuidiwendu", zuidiwendu);
                    values.put("zuigaowendu", zuigaowendu);
                    values.put("zuidishidu", zuidishidu);
                    values.put("zuigaoshidu", zuigaoshidu);
                    values.put("zuixiaotongfeng", zuixiaotongfeng);
                    values.put("zuidatongfeng", zuigaotongfeng);
                    values.put("fuya", fuya);
                    DataSupport.updateAll(WriteBean.class, values , "houseid = ?",WriteDActivity.list.get(activity.mViewPager.getCurrentItem()).getHouseid()+"");
                    activity.mViewPager.setCurrentItem(activity.mViewPager.getCurrentItem() + 1);
                showToast("保存成功");

//                } else if (activity.mViewPager.getCurrentItem() == (activity.mViewPager.getAdapter().getCount() - 1)) {
//                    WriteBean writeBean = new WriteBean();
//                    writeBean.setHouseid(WriteDActivity.list.get(activity.mViewPager.getCurrentItem()).getHouseid());
//                    writeBean.setSiwang(siwang);
//                    writeBean.setTaotai(taotai);
//                    writeBean.setYinshuiliang(yinshuiliang);
//                    writeBean.setTizhong(tizhong);
//                    writeBean.setZuidiwendu(zuidiwendu);
//                    writeBean.setZuigaowendu(zuigaowendu);
//                    writeBean.setZuidishidu(zuidishidu);
//                    writeBean.setZuigaoshidu(zuigaoshidu);
//                    writeBean.setZuixiaotongfeng(zuixiaotongfeng);
//                    writeBean.setZuidatongfeng(zuigaotongfeng);
//                    writeBean.setFuya(fuya);
//                    WriteDActivity.tijiaoList.add(writeBean);

//                    ContentValues values = new ContentValues();
//                    values.put("siwang", siwang);
//                    values.put("taotai", taotai);
//                    values.put("yinshuiliang", yinshuiliang);
//                    values.put("tizhong", tizhong);
//                    values.put("zuidiwendu", zuidiwendu);
//                    values.put("zuigaowendu", zuigaowendu);
//                    values.put("zuidishidu", zuidishidu);
//                    values.put("zuigaoshidu", zuigaoshidu);
//                    values.put("zuixiaotongfeng", zuixiaotongfeng);
//                    values.put("zuidatongfeng", zuigaotongfeng);
//                    values.put("fuya", fuya);
//                    DataSupport.updateAll(WriteBean.class, values , "houseid = ?",WriteDActivity.list.get(activity.mViewPager.getCurrentItem()).getHouseid()+"");
//                    mWrite =  DataSupport.findAll(WriteBean.class);
//                    commit();
//                }

            }
        });

        frag_tijiao_zuihou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mWrite =  DataSupport.findAll(WriteBean.class);
                commit();
            }
        });
    }

//    private RpcObject commitrpcobj;
    private void commit() {
        ViewOnClickTask updateTask = new ViewOnClickTask(getActivity(), "加载中...") {


            @Override
            protected void successUI() {
                showToast("提交成功");
                check();
            }

            @Override
            public Object call() throws Exception {

                ///< 带多样参数的集合
                RpcObject parmObject = WriteDActivity.rpcObj;

                try {
                    for (int i = 0; i <  mWrite.size(); i++) {
                        RpcObject setDetails = parmObject.getManyList("Details").get(i);

                        if (mWrite.get(i).getSiwang() != null && !"".equals(mWrite.get(i).getSiwang())) {
                            setDetails.setInt("DieNumber", Integer.parseInt(mWrite.get(i).getSiwang()));
                        } else {
                            setDetails.setInt("DieNumber", null);
                        }

                        if (mWrite.get(i).getTaotai() != null && !"".equals(mWrite.get(i).getTaotai())) {
                            setDetails.setInt("Obsolete", Integer.parseInt(mWrite.get(i).getTaotai()));
                        } else {
                            setDetails.setInt("Obsolete", null);
                        }

                        if (mWrite.get(i).getYinshuiliang() != null && !"".equals(mWrite.get(i).getYinshuiliang())) {
                            setDetails.setDecimal("WaterIntake", new BigDecimal(mWrite.get(i).getYinshuiliang()));
                        } else {
                            setDetails.setDecimal("WaterIntake", null);
                        }

                        if (mWrite.get(i).getTizhong() != null && !"".equals(mWrite.get(i).getTizhong())) {
                            setDetails.setDecimal("Weight", new BigDecimal(mWrite.get(i).getTizhong()));
                        } else {
                            setDetails.setDecimal("Weight", null);
                        }

                        if (mWrite.get(i).getZuidiwendu() != null && !"".equals(mWrite.get(i).getZuidiwendu())) {
                            setDetails.setDecimal("MinTemperature", new BigDecimal(mWrite.get(i).getZuidiwendu()));
                        } else {
                            setDetails.setDecimal("MinTemperature", null);
                        }

                        if (mWrite.get(i).getZuigaowendu() != null && !"".equals(mWrite.get(i).getZuigaowendu())) {
                            setDetails.setDecimal("MaxTemperature", new BigDecimal(mWrite.get(i).getZuigaowendu()));
                        } else {
                            setDetails.setDecimal("MaxTemperature", null);
                        }

                        if (mWrite.get(i).getZuidishidu() != null && !"".equals(mWrite.get(i).getZuidishidu())) {
                            setDetails.setDecimal("MinHumidity", new BigDecimal(mWrite.get(i).getZuidishidu()));
                        } else {
                            setDetails.setDecimal("MinHumidity", null);
                        }

                        if (mWrite.get(i).getFuya() != null && !"".equals(mWrite.get(i).getFuya())) {
                            setDetails.setString("NegativePressure", mWrite.get(i).getFuya());
                        } else {
                            setDetails.setString("NegativePressure", null);
                        }

                        if (mWrite.get(i).getZuigaoshidu() != null && !"".equals(mWrite.get(i).getZuigaoshidu())) {
                            setDetails.setDecimal("MaxHumidity", new BigDecimal(mWrite.get(i).getZuigaoshidu()));
                        } else {
                            setDetails.setDecimal("MaxHumidity", null);
                        }
                        if (mWrite.get(i).getZuixiaotongfeng() != null && !"".equals(mWrite.get(i).getZuixiaotongfeng())) {
                            setDetails.setString("NightMinAirQuantity", mWrite.get(i).getZuixiaotongfeng());
                        } else {
                            setDetails.setString("NightMinAirQuantity", null);
                        }

                        if (mWrite.get(i).getZuidatongfeng() != null && !"".equals(mWrite.get(i).getZuidatongfeng())) {
                            setDetails.setString("DayMaxAirQuantity", mWrite.get(i).getZuidatongfeng());
                        } else {
                            setDetails.setString("DayMaxAirQuantity", null);
                        }
                    }

                    JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.DayProductRpc_Update, parmObject);
                    WriteDActivity.rpcObj = result.getRpcObject(RpcUrl.DayProductRpc_Update_canshu_fanhui);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
                return null;
            }
        };
        updateTask.execute();
    }

    private void check() {
        ViewOnClickTask checkTask = new ViewOnClickTask(getActivity(),"加载中...") {
//            RpcObject rpcobj;

            @Override
            protected void successUI() {
                showToast("审核成功");
                activity.finish();
            }

            @Override
            public Object call() throws Exception {
                ///< 带多样参数的集合
                RpcObject parmObject = WriteDActivity.rpcObj;
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.DayProductRpc_check, parmObject);
                WriteDActivity.rpcObj = result.getRpcObject(RpcUrl.DayProductRpc_check_canshu_fanhui);
                return null;
            }
        };
        checkTask.execute();

    }

    @Override
    public void onResume() {
        super.onResume();
        List<WriteBean> dewrb = DataSupport.findAll(WriteBean.class);

        frag_death.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getSiwang());
        frag_eliminate.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getTaotai());
        frag_water.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getYinshuiliang());
        frag_weight.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getTizhong());


        if ("".equals(dewrb.get(activity.mViewPager.getCurrentItem()).getZuidiwendu()) || dewrb.get(activity.mViewPager.getCurrentItem()).getZuidiwendu() == null) {
            frag_mint.setText(dewrb.get(0).getZuidiwendu());
        } else {
            frag_mint.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getZuidiwendu());
        }

        if ("".equals(dewrb.get(activity.mViewPager.getCurrentItem()).getZuigaowendu()) || dewrb.get(activity.mViewPager.getCurrentItem()).getZuigaowendu() == null) {
            frag_maxt.setText(dewrb.get(0).getZuigaowendu());
        } else {
            frag_maxt.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getZuigaowendu());
        }

        if ("".equals(dewrb.get(activity.mViewPager.getCurrentItem()).getZuidishidu()) || dewrb.get(activity.mViewPager.getCurrentItem()).getZuidishidu() == null) {
            frag_minh.setText(dewrb.get(0).getZuidishidu());
        } else {
            frag_minh.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getZuidishidu());
        }

        if ("".equals(dewrb.get(activity.mViewPager.getCurrentItem()).getZuigaoshidu()) || dewrb.get(activity.mViewPager.getCurrentItem()).getZuigaoshidu() == null) {
            frag_maxh.setText(dewrb.get(0).getZuigaoshidu());
        } else {
            frag_maxh.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getZuigaoshidu());
        }

        if ("".equals(dewrb.get(activity.mViewPager.getCurrentItem()).getZuixiaotongfeng()) || dewrb.get(activity.mViewPager.getCurrentItem()).getZuixiaotongfeng() == null) {
            frag_minw.setText(dewrb.get(0).getZuixiaotongfeng());
        } else {
            frag_minw.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getZuixiaotongfeng());
        }

        if ("".equals(dewrb.get(activity.mViewPager.getCurrentItem()).getZuidatongfeng()) || dewrb.get(activity.mViewPager.getCurrentItem()).getZuidatongfeng() == null) {
            frag_maxw.setText(dewrb.get(0).getZuidatongfeng());
        } else {
            frag_maxw.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getZuidatongfeng());
        }
        if ("".equals(dewrb.get(activity.mViewPager.getCurrentItem()).getFuya()) || dewrb.get(activity.mViewPager.getCurrentItem()).getFuya() == null) {
            frag_pressure.setText(dewrb.get(0).getFuya());
        } else {
            frag_pressure.setText(dewrb.get(activity.mViewPager.getCurrentItem()).getFuya());
        }

        if (activity.mViewPager.getCurrentItem() == (activity.mViewPager.getAdapter().getCount() - 1)) {
            frag_tijiao_zuihou.setVisibility(View.VISIBLE);
        }
    }
}
