package com.qhclh.ytzh.work.tablepoultry.changle;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseFragment;
import com.qhclh.ytzh.bean.TablePBean;
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 com.qhclh.ytzh.work.tablepoultry.ReportActivity;
import com.qhclh.ytzh.work.tablepoultry.XFactryAdapter;

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 java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;

/**
 * Created by 青花瓷 on 2017/12/4.
 */

public class ChangleFragment extends BaseFragment {
    @BindView(R.id.lv_changle)
    ListView lv_changle;

    private List<TablePBean> list;
    private XFactryAdapter adapter;

    private static final String ARG_TIMELINE_TYPE = "ARG_TIMELINE_TYPE";
    public static ChangleFragment newInstance(long type,long choseTime) {
        Bundle args = new Bundle();
        args.putLong(ARG_TIMELINE_TYPE, type);
        args.putLong("time",choseTime);
        ChangleFragment fragment = new ChangleFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private long mType;
    private long choseDate;

    @Override
    protected int setLayout() {
        return R.layout.fragment_changle;
    }

    @Override
    protected void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        list = new ArrayList<>();
    }

    @Override
    protected void initData() {
        mType = getArguments().getLong(ARG_TIMELINE_TYPE);
        choseDate = getArguments().getLong("time");
    }

    @Override
    protected void initOper() {
        lv_changle.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                startActivity(new Intent(getActivity(), ReportActivity.class));
            }
        });
    }


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

    private void BreedFactoryReportListView(final long time) {
        ViewOnClickTask listTask = new ViewOnClickTask(getActivity(),"加载中...") {
            List<RpcObject> rpcList;
            @Override
            protected void successUI() {
                list.clear();
                System.out.println("aaa++++"+rpcList);
                for (int i=0;i<rpcList.size();i++){
                    list.add(new TablePBean(mType,rpcList.get(i).getLong("ID"),rpcList.get(i).getString("Name"),"22b"+mType,"333c"+mType,"5555d"+mType));
                }

                adapter = new XFactryAdapter(getActivity(),list);
                lv_changle.setAdapter(adapter);
            }

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


    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onEvent(ChoseEvent event){
       System.out.println("aaa++++bus+++"+event.getTime());
        BreedFactoryReportListView(event.getTime());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
