package com.qhclh.ytzh.work.productiondailyw;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

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.ui.CalendarDialogFragment;

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 2017/12/4.
 */

public class BasicattributesFragment extends BaseFragment {
    @BindView(R.id.basicattr_farm)
    Spinner basicattr_farm;
    @BindView(R.id.basicattr_batch)
    Spinner basicattr_batch;
    @BindView(R.id.basicattr_account)
    Spinner basicattr_account;
    public static TextView basicattr_date;

    private List<String> farmnameList=new ArrayList<>();
    private List<String> farmidList=new ArrayList<>();
    private List<String> batchnameList=new ArrayList<>();
    private List<String> batchidList=new ArrayList<>();
    private List<String> accountnameList=new ArrayList<>();
    private List<String> accountidList=new ArrayList<>();
    private String farmid;

    private ProductiondailywActivity act;
    public static List<String> houseNameList = new ArrayList<>();
    public static List<String> houseIdList = new ArrayList<>();
    @Override
    protected int setLayout() {
        return R.layout.fragment_basicattr;
    }

    @Override
    protected void initView() {
        basicattr_date = getActivity().findViewById(R.id.basicattr_date);
    }

    @Override
    protected void initData() {
        act = (ProductiondailywActivity) getActivity();
    }

    @Override
    protected void initOper() {
        basicattr_farm.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                farmid = farmidList.get(i);
                act.setFarmId(Long.parseLong(farmid));
                batch();
                house();

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        basicattr_batch.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                act.setBatchId(Long.parseLong(batchidList.get(i)));
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        basicattr_account.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                act.setAccountId(Long.parseLong(accountidList.get(i)));
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        basicattr_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new CalendarDialogFragment().show(getFragmentManager(), "start");

            }
        });

    }

    private void house() {
        ViewOnClickTask houseTask = new ViewOnClickTask(getActivity(),"加载中...") {
            List<RpcObject> rpcobj;
            @Override
            protected void successUI() {
                int size = rpcobj.size();
                houseIdList.clear();
                houseNameList.clear();
                for (int i=0;i<size;i++){
                    houseNameList.add(rpcobj.get(i).getString("Item2"));
                    houseIdList.add(rpcobj.get(i).getString("Item1"));
                }

            }

            @Override
            public Object call() throws Exception {


                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.GetChoiceBoxWords,"B3ProduceUnitedInfos_栋舍","",farmid);
                rpcobj = result.getRpcObjectList(RpcUrl.GetChoiceBoxWords_fanhui);
                return null;
            }
        };
        houseTask.execute();
    }

    @Override
    public void onResume() {
        super.onResume();
        farm();
        account();
    }

    private void account() {
        ViewOnClickTask accountTask = new ViewOnClickTask(getActivity(),"加载中...") {
            List<RpcObject> rpcobj;
            @Override
            protected void successUI() {
                int size = rpcobj.size();
                accountnameList.clear();
                accountidList.clear();
                for (int i=0;i<size;i++){
                    accountnameList.add(rpcobj.get(i).getString("Item2"));
                    accountidList.add(rpcobj.get(i).getString("Item1"));
                }
                ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,accountnameList);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                basicattr_account.setAdapter(adapter);
            }

            @Override
            public Object call() throws Exception {


                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.GetChoiceBoxWords,"授权会计单位全部","","");
                rpcobj = result.getRpcObjectList(RpcUrl.GetChoiceBoxWords_fanhui);
                return null;
            }
        };
        accountTask.execute();
    }

    private void batch() {
        ViewOnClickTask batchTask = new ViewOnClickTask(getActivity(),"加载中...") {
            List<RpcObject> rpcobj;
            @Override
            protected void successUI() {
                int size = rpcobj.size();
                batchnameList.clear();
                batchidList.clear();
                for (int i=0;i<size;i++){
                    batchnameList.add(rpcobj.get(i).getString("Item2"));
                    batchidList.add(rpcobj.get(i).getString("Item1"));
                }
                ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,batchnameList);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                basicattr_batch.setAdapter(adapter);
            }

            @Override
            public Object call() throws Exception {


                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.GetChoiceBoxWords,"B3PoultryBreed_批次","",farmid);
                rpcobj = result.getRpcObjectList(RpcUrl.GetChoiceBoxWords_fanhui);
                return null;
            }
        };
        batchTask.execute();
    }

    private void farm() {
        ViewOnClickTask farmTask = new ViewOnClickTask(getActivity(),"加载中...") {
            List<RpcObject> rpcobj;
            @Override
            protected void successUI() {
                int size = rpcobj.size();
                farmnameList.clear();
                farmidList.clear();

                for (int i=0;i<size;i++){
                    farmnameList.add(rpcobj.get(i).getString("Item2"));
                    farmidList.add(rpcobj.get(i).getString("Item1"));
                }
                ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,farmnameList);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                basicattr_farm.setAdapter(adapter);
            }

            @Override
            public Object call() throws Exception {


                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.GetChoiceBoxWords,"B3ProduceUnitedInfos_养殖场","","");
                rpcobj = result.getRpcObjectList(RpcUrl.GetChoiceBoxWords_fanhui);
                return null;
            }
        };
        farmTask.execute();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }
}
