package com.qhclh.ytzh.index.shangpin;

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 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();
                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("");
                        }
                        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("");
                        }
                        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);
                    pianqu_name.setText((i+1)+".养殖场:"+cunlanlist.get(i).getName());
                    pianqu_content.setText(cunlanlist.get(i).getContact());
                    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);
                    pianqu_name.setText((i+1)+".养殖场:"+konglanlist.get(i).getName());
                    pianqu_content.setText(konglanlist.get(i).getContact());
                    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();
    }
}
