package com.qhclh.ytzh.work.tablepoultry.changle;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseFragment;
import com.qhclh.ytzh.bean.TablePBean;
import com.qhclh.ytzh.home.RpcUrl;
import com.qhclh.ytzh.tasks.ViewOnClickTask;
import com.qhclh.ytzh.utils.DateTimeUtil;
import com.qhclh.ytzh.work.tablepoultry.ChoseEvent;
import com.qhclh.ytzh.work.tablepoultry.ReportActivity;
import com.qhclh.ytzh.work.tablepoultry.XFactryAdapter;

import org.forks.jsonrpc.JsonRpcException;
import org.forks.jsonrpc.JsonRpcResult;
import org.forks.jsonrpc.RpcFacade;
import org.forks.jsonrpc.RpcObject;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

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

import butterknife.BindView;

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

public class ChangleFragment extends BaseFragment {
    @BindView(R.id.lv_changle)
    ListView lv_changle;

    private List<TablePBean> list;
    private XFactryAdapter adapter;

    private static final String ARG_TIMELINE_TYPE = "ARG_TIMELINE_TYPE";
    public static ChangleFragment newInstance(long type,long choseTime) {
        Bundle args = new Bundle();
        args.putLong(ARG_TIMELINE_TYPE, type);
        args.putLong("time",choseTime);
        ChangleFragment fragment = new ChangleFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private long mType;
    private long choseDate=System.currentTimeMillis()- 1000 * 60 * 60 * 24;

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

    @Override
    protected void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        list = new ArrayList<>();
    }

    @Override
    protected void initData() {
        mType = getArguments().getLong(ARG_TIMELINE_TYPE);
    }

    @Override
    protected void initOper() {
        lv_changle.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getActivity(), ReportActivity.class);
                intent.putExtra("BreedFactory_id",l);
                intent.putExtra("time",choseDate);
                startActivity(intent);
            }
        });
    }


    @Override
    public void onResume() {
        super.onResume();
        BreedFactoryReportListView(choseDate);
    }

    private void BreedFactoryReportListView(final long time) {
        final ViewOnClickTask listTask = new ViewOnClickTask(getActivity(),"加载中...") {
            List<RpcObject> rpcList;
            @Override
            protected void successUI() {
                list.clear();
                List<RpcObject> detailsList;
                for (int i=0;i<rpcList.size();i++){
                    try {
                        RpcObject details = RpcObject.create(RpcUrl.BreedFactoryReportListView_detail);
                        details.getInt("Days");
                        details.getInt("LiveNumber");
                        details.getInt("DieNumber");
                        details.getInt("ObsoleteNumber");

                        detailsList = (List<RpcObject>) rpcList.get(i).get("Details");

                        TablePBean tb = new TablePBean();
                        tb.setBreedFactory_id(rpcList.get(i).getLong("ID"));
                        tb.setBreedFactory_name(rpcList.get(i).getString("Name"));

                        for (int j=0;j<detailsList.size();j++){
                            TablePBean.Details details1b = new TablePBean.Details();

                            if (detailsList.size()>0) {
                                if (detailsList.get(0).getInt("Days") != null) {
                                    details1b.setDays1(detailsList.get(0).getInt("Days") + "");
                                }
                                if (detailsList.get(0).getInt("DieNumber") != null) {
                                    details1b.setDienumber1(detailsList.get(0).getInt("DieNumber") + "");
                                }
                                if (detailsList.get(0).getInt("ObsoleteNumber") != null) {
                                    details1b.setObs1(detailsList.get(0).getInt("ObsoleteNumber") + "");
                                }
                                if (detailsList.get(0).getInt("LiveNumber") != null) {
                                    details1b.setCunhuo1(detailsList.get(0).getInt("LiveNumber") + "");
                                }
                                details1b.setChenghuolv1("10.00%");
                                if (detailsList.size()>1) {
                                    if (detailsList.get(1).getInt("Days") != null) {
                                        details1b.setDays2(detailsList.get(1).getInt("Days") + "");
                                    }
                                    if (detailsList.get(1).getInt("DieNumber") != null) {
                                        details1b.setDienumber2(detailsList.get(1).getInt("DieNumber") + "");
                                    }
                                    if (detailsList.get(1).getInt("ObsoleteNumber") != null) {
                                        details1b.setObs2(detailsList.get(1).getInt("ObsoleteNumber") + "");
                                    }
                                    if (detailsList.get(1).getInt("LiveNumber") != null) {
                                        details1b.setCunhuo2(detailsList.get(1).getInt("LiveNumber") + "");
                                    }
                                    details1b.setChenghuolv2("20.00%");
                                    if (detailsList.size()>2){
                                        if (detailsList.get(2).getInt("Days") != null) {
                                            details1b.setDays3(detailsList.get(2).getInt("Days") + "");
                                        }
                                        if (detailsList.get(2).getInt("DieNumber") != null) {
                                            details1b.setDienumber3(detailsList.get(2).getInt("DieNumber") + "");
                                        }
                                        if (detailsList.get(2).getInt("ObsoleteNumber") != null) {
                                            details1b.setObs3(detailsList.get(2).getInt("ObsoleteNumber") + "");
                                        }
                                        if (detailsList.get(2).getInt("LiveNumber") != null) {
                                            details1b.setCunhuo3(detailsList.get(2).getInt("LiveNumber") + "");
                                        }
                                        details1b.setChenghuolv3("30.00%");
                                        if (detailsList.size()>3){
                                            if (detailsList.get(3).getInt("Days") != null) {
                                                details1b.setDays4(detailsList.get(3).getInt("Days") + "");
                                            }
                                            if (detailsList.get(3).getInt("DieNumber") != null) {
                                                details1b.setDienumber4(detailsList.get(3).getInt("DieNumber") + "");
                                            }
                                            if (detailsList.get(3).getInt("ObsoleteNumber") != null) {
                                                details1b.setObs4(detailsList.get(3).getInt("ObsoleteNumber") + "");
                                            }
                                            if (detailsList.get(3).getInt("LiveNumber") != null) {
                                                details1b.setCunhuo4(detailsList.get(3).getInt("LiveNumber") + "");
                                            }
                                            details1b.setChenghuolv4("40.00%");
                                            if (detailsList.size()>4){
                                                if (detailsList.get(4).getInt("Days") != null) {
                                                    details1b.setDays5(detailsList.get(4).getInt("Days") + "");
                                                }
                                                if (detailsList.get(4).getInt("DieNumber") != null) {
                                                    details1b.setDienumber5(detailsList.get(4).getInt("DieNumber") + "");
                                                }
                                                if (detailsList.get(4).getInt("ObsoleteNumber") != null) {
                                                    details1b.setObs5(detailsList.get(4).getInt("ObsoleteNumber") + "");
                                                }
                                                if (detailsList.get(4).getInt("LiveNumber") != null) {
                                                    details1b.setCunhuo5(detailsList.get(4).getInt("LiveNumber") + "");
                                                }
                                                details1b.setChenghuolv5("50.00%");
                                                if (detailsList.size()>5){
                                                    if (detailsList.get(5).getInt("Days") != null) {
                                                        details1b.setDays6(detailsList.get(5).getInt("Days") + "");
                                                    }
                                                    if (detailsList.get(5).getInt("DieNumber") != null) {
                                                        details1b.setDienumber6(detailsList.get(5).getInt("DieNumber") + "");
                                                    }
                                                    if (detailsList.get(5).getInt("ObsoleteNumber") != null) {
                                                        details1b.setObs6(detailsList.get(5).getInt("ObsoleteNumber") + "");
                                                    }
                                                    if (detailsList.get(5).getInt("LiveNumber") != null) {
                                                        details1b.setCunhuo6(detailsList.get(5).getInt("LiveNumber") + "");
                                                    }
                                                    details1b.setChenghuolv6("60.00%");
                                                    if (detailsList.size()>6){
                                                        if (detailsList.get(6).getInt("Days") != null) {
                                                            details1b.setDays7(detailsList.get(6).getInt("Days") + "");
                                                        }
                                                        if (detailsList.get(6).getInt("DieNumber") != null) {
                                                            details1b.setDienumber7(detailsList.get(6).getInt("DieNumber") + "");
                                                        }
                                                        if (detailsList.get(6).getInt("ObsoleteNumber") != null) {
                                                            details1b.setObs7(detailsList.get(6).getInt("ObsoleteNumber") + "");
                                                        }
                                                        if (detailsList.get(6).getInt("LiveNumber") != null) {
                                                            details1b.setCunhuo7(detailsList.get(6).getInt("LiveNumber") + "");
                                                        }
                                                        details1b.setChenghuolv7("70.00%");
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }

                            }

                            tb.setDetailses(details1b);
                        }

                        list.add(tb);
                    } catch (JsonRpcException e) {
                        e.printStackTrace();
                    }

                }

                adapter = new XFactryAdapter(getActivity(),list);
                lv_changle.setAdapter(adapter);
            }

            @Override
            public Object call() throws Exception {
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.BreedFactoryReportListView,mType,new SimpleDateFormat("yyyy-MM-dd").parse(DateTimeUtil.getYMD(new Date(time))));
                rpcList = result.getRpcObjectList(RpcUrl.BreedFactoryReportListView_fanhui);
                return null;
            }
        };
        listTask.execute();
    }


    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onEvent(ChoseEvent event){
        choseDate = event.getTime();
        BreedFactoryReportListView(event.getTime());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        list.clear();
        choseDate=0;
        if (EventBus.getDefault().getStickyEvent(ChoseEvent.class) != null){
            EventBus.getDefault().removeStickyEvent(ChoseEvent.class);
        }

    }
}
