package com.qhclh.ytzh.index.shangpin;

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

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseActivity;
import com.qhclh.ytzh.bean.BreedFactoryInfoBean;
import com.qhclh.ytzh.home.RpcUrl;
import com.qhclh.ytzh.tasks.ViewOnClickTask;
import com.qhclh.ytzh.utils.DateTimeUtil;
import com.qhclh.ytzh.work.tablepoultry.ReportActivity;

import org.forks.jsonrpc.JsonRpcResult;
import org.forks.jsonrpc.RpcFacade;
import org.forks.jsonrpc.RpcObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by 青花瓷 on 2018/1/24.
 */

public class PianquinfosActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_toolbar_title)
    TextView tv_toolbar_title;
    private long id;
    private String name;
    @BindView(R.id.ll_cunlan)
    LinearLayout ll_cunlan;
    @BindView(R.id.ll_konglan)
    LinearLayout ll_konglan;

    private List<BreedFactoryInfoBean> cunlanlist;
    private List<BreedFactoryInfoBean> konglanlist;

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

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

    }

    @Override
    protected void initData() {
        id = getIntent().getLongExtra("id",0);
        name = getIntent().getStringExtra("name");
        tv_toolbar_title.setText(name+"片区详细");

        cunlanlist = new ArrayList<>();
        konglanlist = new ArrayList<>();
    }

    @Override
    protected void initOper() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getFactryList();
    }

    private void getFactryList() {
        ViewOnClickTask factryTask = new ViewOnClickTask(this,"加载中...") {
            List<RpcObject> rpcObjectList;
            @Override
            protected void successUI() {
                cunlanlist.clear();
                konglanlist.clear();

                ll_cunlan.removeAllViews();
                ll_konglan.removeAllViews();
                for (int i=0;i<rpcObjectList.size();i++)
                {
                    if (rpcObjectList.get(i).getBoolean("Empty")==true){
                        ///< 空栏
                        BreedFactoryInfoBean konglanbean = new BreedFactoryInfoBean();
                        konglanbean.setID(rpcObjectList.get(i).getLong("ID"));
                        if (rpcObjectList.get(i).getString("Name")!=null) {
                            konglanbean.setName(rpcObjectList.get(i).getString("Name"));
                        }else {
                            konglanbean.setName("");
                        }
                        if (rpcObjectList.get(i).getString("Contact")!=null) {
                            konglanbean.setContact(rpcObjectList.get(i).getString("Contact"));
                        }else {
                            konglanbean.setContact("");
                        }

                        if (rpcObjectList.get(i).getDate("PlanInDate") !=null){
                            konglanbean.setPlanInDate(DateTimeUtil.getYMD2(rpcObjectList.get(i).getDate("PlanInDate")));
                        }else {
                            konglanbean.setPlanInDate("");
                        }
                        konglanlist.add(konglanbean);

                    }else {
                        ///< 存栏
                        BreedFactoryInfoBean cunlanbean = new BreedFactoryInfoBean();
                        cunlanbean.setID(rpcObjectList.get(i).getLong("ID"));
                        if (rpcObjectList.get(i).getString("Name")!=null) {
                            cunlanbean.setName(rpcObjectList.get(i).getString("Name"));
                        }else {
                            cunlanbean.setName("");
                        }
                        if (rpcObjectList.get(i).getString("Contact")!=null) {
                            cunlanbean.setContact(rpcObjectList.get(i).getString("Contact"));
                        }else {
                            cunlanbean.setContact("");
                        }

                        if (rpcObjectList.get(i).getDate("PlanOutDate")!=null){
                            cunlanbean.setPlanOutDate(DateTimeUtil.getYMD2(rpcObjectList.get(i).getDate("PlanOutDate")));
                        }else {
                            cunlanbean.setPlanOutDate("");
                        }
                        cunlanlist.add(cunlanbean);
                    }
                }


                for (int i=0;i<cunlanlist.size();i++){
                    View view = LayoutInflater.from(PianquinfosActivity.this).inflate(R.layout.item_pianquinfos,null);
                    TextView pianqu_name = view.findViewById(R.id.pianqu_name);
                    TextView pianqu_content = view.findViewById(R.id.pianqu_content);
                    LinearLayout pianqu_ll = view.findViewById(R.id.pianqu_ll);

                    pianqu_ll.setTag(cunlanlist.get(i).getID());
                    pianqu_ll.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(PianquinfosActivity.this, ReportActivity.class);
                            intent.putExtra("BreedFactory_id", (Long) view.getTag());
                            intent.putExtra("time",System.currentTimeMillis()- 1000 * 60 * 60 * 24);
                            startActivity(intent);
                        }
                    });
                    pianqu_name.setText((i+1)+"."+cunlanlist.get(i).getName()+"("+cunlanlist.get(i).getContact()+")");
                    pianqu_content.setText("计划出栏:"+cunlanlist.get(i).getPlanOutDate());
                    ll_cunlan.addView(view);
                }
                for (int i=0;i<konglanlist.size();i++){
                    View view = LayoutInflater.from(PianquinfosActivity.this).inflate(R.layout.item_pianquinfos,null);
                    TextView pianqu_name = view.findViewById(R.id.pianqu_name);
                    TextView pianqu_content = view.findViewById(R.id.pianqu_content);
//                    LinearLayout pianqu_ll = view.findViewById(R.id.pianqu_ll);
//                    pianqu_ll.setTag(konglanlist.get(i).getID());
//                    pianqu_ll.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View view) {
//                            Intent intent = new Intent(PianquinfosActivity.this, ReportActivity.class);
//                            intent.putExtra("BreedFactory_id", (Long) view.getTag());
//                            intent.putExtra("time",System.currentTimeMillis()- 1000 * 60 * 60 * 24);
//                            startActivity(intent);
//                        }
//                    });

                    pianqu_name.setText((i+1)+"."+konglanlist.get(i).getName()+"("+konglanlist.get(i).getContact()+")");
                    pianqu_content.setText("计划上鸡:"+konglanlist.get(i).getPlanInDate());
                    ll_konglan.addView(view);
                }

            }

            @Override
            public Object call() throws Exception {
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.BreedFactoryDetails,id);
                rpcObjectList = result.getRpcObjectList(RpcUrl.BreedFactoryDetails_fanhui);
                return null;
            }
        };
        factryTask.execute();
    }
}
