package com.qhclh.ytzh.index.fumudai;

import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseActivity;
import com.qhclh.ytzh.bean.ThreeTypeBean;
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 java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

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

public class ThreeTypeActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_toolbar_title)
    TextView tv_toolbar_title;

    @BindView(R.id.lv_threetype)
    ListView lv_threetype;

    private long status;
    private ThreeTypeAdapter threeTypeAdapter;
    private List<ThreeTypeBean> threeTypeBeanList;
    @Override
    protected int setLayoutId() {
        return R.layout.act_threetype;
    }

    @Override
    protected void initView() {
        initToolbar(mToolbar, "", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        status = getIntent().getLongExtra("status",-1);
        threeTypeBeanList = new ArrayList<>();
    }

    @Override
    protected void initOper() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (status ==0){
            tv_toolbar_title.setText("空栏");
            BreedFactoryCountView0();
        }else if (status == 1){
            tv_toolbar_title.setText("育雏育成期");
            BreedFactoryCountView1();
        }else if (status == 2){
            tv_toolbar_title.setText("产蛋期");
            BreedFactoryCountView2();
        }
    }

    private void BreedFactoryCountView0() {
        ViewOnClickTask breedTask = new ViewOnClickTask(ThreeTypeActivity.this,"加载中...") {
            List<RpcObject> rpcList0;
            @Override
            protected void successUI() {
                threeTypeBeanList.clear();
                for (int i=0;i<rpcList0.size();i++){
                    threeTypeBeanList.add(new ThreeTypeBean(rpcList0.get(i).getLong("BreedFactory_ID")
                            ,rpcList0.get(i).getString("BreedFactory_Name")
                            ,"在养栋舍数: "+rpcList0.get(i).getInt("LivingNumber"),"未养栋舍数: "+rpcList0.get(i).getInt("EmptyNumber")));
                }
                threeTypeAdapter = new ThreeTypeAdapter(ThreeTypeActivity.this,threeTypeBeanList);
                lv_threetype.setAdapter(threeTypeAdapter);
            }

            @Override
            public Object call() throws Exception {
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.BreedFactoryCountView0);
                rpcList0 = result.getRpcObjectList(RpcUrl.BreedFactoryCountView0_fanhui);
                return null;
            }
        };
        breedTask.execute();
    }

    private void BreedFactoryCountView1() {
        ViewOnClickTask breedTask1 = new ViewOnClickTask(ThreeTypeActivity.this,"加载中...") {
            List<RpcObject> rpcList1;
            @Override
            protected void successUI() {
                threeTypeBeanList.clear();
                String male="";
                String female = "";
                for (int i=0;i<rpcList1.size();i++){
                    if (rpcList1.get(i).getInt("MaleNumber")!=null){
                        male = rpcList1.get(i).getInt("MaleNumber").toString();
                    }
                    if (rpcList1.get(i).getInt("FemaleNumber")!=null){
                        female = rpcList1.get(i).getInt("FemaleNumber").toString();
                    }

                    threeTypeBeanList.add(new ThreeTypeBean(rpcList1.get(i).getLong("BreedFactory_ID")
                            ,rpcList1.get(i).getString("BreedFactory_Name")
                            ,"公存栏数: "+male,"母存栏数: "+female));
                }
                threeTypeAdapter = new ThreeTypeAdapter(ThreeTypeActivity.this,threeTypeBeanList);
                lv_threetype.setAdapter(threeTypeAdapter);
            }

            @Override
            public Object call() throws Exception {
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.BreedFactoryCountView1);
                rpcList1 = result.getRpcObjectList(RpcUrl.BreedFactoryCountView1_fanhui);
                return null;
            }
        };
        breedTask1.execute();
    }

    private void BreedFactoryCountView2() {
        ViewOnClickTask breedTask2 = new ViewOnClickTask(ThreeTypeActivity.this,"加载中...") {
            List<RpcObject> rpcList2;
            @Override
            protected void successUI() {
                threeTypeBeanList.clear();
                String eggR = "";
                String female = "";
                for (int i=0;i<rpcList2.size();i++){
                    if (rpcList2.get(i).getDecimal("EggRate")!=null){
                        eggR = rpcList2.get(i).getDecimal("EggRate").multiply(new BigDecimal("100")).stripTrailingZeros().toPlainString()+"%";
                    }
                    if (rpcList2.get(i).getInt("FemaleNumber")!=null){
                        female = rpcList2.get(i).getInt("FemaleNumber").toString();
                    }
                    threeTypeBeanList.add(new ThreeTypeBean(rpcList2.get(i).getLong("BreedFactory_ID")
                            ,rpcList2.get(i).getString("BreedFactory_Name")
                            ,"产蛋率: "+eggR,"母存栏数: "+female));
                }
                threeTypeAdapter = new ThreeTypeAdapter(ThreeTypeActivity.this,threeTypeBeanList);
                lv_threetype.setAdapter(threeTypeAdapter);
            }

            @Override
            public Object call() throws Exception {
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.BreedFactoryCountView2);
                rpcList2 = result.getRpcObjectList(RpcUrl.BreedFactoryCountView2_fanhui);
                return null;
            }
        };
        breedTask2.execute();
    }
}
