package com.qhclh.ytzh.work.Poultrydailyreport;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseFragment;
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 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 org.json.JSONArray;

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

import butterknife.BindView;

/**
 * Created by 青花瓷 on 2018/3/22.
 */

public class YuchuFragment extends BaseFragment {
    @BindView(R.id.firstContent)
    LinearLayout firstContent;
    private long choseDate=System.currentTimeMillis()- 1000 * 60 * 60 * 24;
    @Override
    protected int setLayout() {
        return R.layout.frag_yuchu;
    }

    @Override
    protected void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initOper() {

    }

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

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

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

    }
    private void BatchTotalCount1(final long time) {
        ViewOnClickTask BatchTotalCount1Task = new ViewOnClickTask(getActivity(),"加载中...") {
            List<RpcObject> rpcStr;
            @Override
            protected void successUI() {
                firstContent.removeAllViews();
                for (int i=0;i<rpcStr.size();i++){
                    View view = LayoutInflater.from(getActivity()).inflate(R.layout.view_first,null);
                    TextView name = view.findViewById(R.id.name);

                    LinearLayout secondContent = view.findViewById(R.id.secondContent);
                    LinearLayout main_ll = view.findViewById(R.id.main_ll);

                    name.setText(rpcStr.get(i).getString("BreedFactory_Name"));
                    main_ll.setTag(rpcStr.get(i).getLong("BreedFactory_ID"));
                    secondContent.removeAllViews();
                    List<RpcObject> detailsList = rpcStr.get(i).getList("Details");
                    for (int j=0;j<detailsList.size();j++){
                        View view2 = LayoutInflater.from(getActivity()).inflate(R.layout.view_second,null);
                        TextView item1 = view2.findViewById(R.id.item1);
                        TextView item2 = view2.findViewById(R.id.item2);
                        TextView item3 = view2.findViewById(R.id.item3);
                        TextView item4 = view2.findViewById(R.id.item4);

                        if (detailsList.get(j).getString("Batch_Name")!=null) {
                            item1.setText("批次名:" + detailsList.get(j).getString("Batch_Name"));
                        }else {
                            item1.setText("批次名:");
                        }
                        if (detailsList.get(j).getString("Week")!=null) {
                            item2.setText("周龄:" + detailsList.get(j).getString("Week"));
                        }else {
                            item2.setText("周龄:");
                        }
                        if (detailsList.get(j).getInt("FemaleSiTaoNumber")!=null) {
                            item3.setText("母死淘:" + detailsList.get(j).getInt("FemaleSiTaoNumber"));
                        }else {
                            item3.setText("母死淘:");
                        }

                        if (detailsList.get(j).getDecimal("FemaleSurvivalRate")!=null) {
                            item4.setText("母成活率:" + detailsList.get(j).getDecimal("FemaleSurvivalRate").multiply(new BigDecimal("100")).stripTrailingZeros().toPlainString() + "%");
                        }else {
                            item4.setText("母成活率:");
                        }
                        secondContent.addView(view2);
                    }
                    firstContent.addView(view);
                    main_ll.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(getActivity(),PoultryReportActvity.class);
                            intent.putExtra("BreedFactory_ID",(long)view.getTag());
                            intent.putExtra("time",choseDate);
                            startActivity(intent);
                        }
                    });
                }
            }

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