package com.qhclh.ytzh.work.weighingsingle;

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

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseActivity;
import com.qhclh.ytzh.home.RpcUrl;
import com.qhclh.ytzh.tasks.ViewOnClickTask;
import com.qhclh.ytzh.ui.CalendarDialogFragment;
import com.qhclh.ytzh.ui.RefreshLayout;

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

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

import butterknife.BindView;

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

public class WeightListActivity extends BaseActivity implements CalendarDialogFragment.CalendarClickListener {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_toolbar_title)
    TextView tv_toolbar_title;
    @BindView(R.id.lv_infos_weight)
    ListView lv_infos_weight;
    @BindView(R.id.rfl_weightlist)
    RefreshLayout rfl_weightlist;
//    private int page =0;

    private WeightlistAdapter adapter;
    private List<WeightBean> list;
    @Override
    protected int setLayoutId() {
        return R.layout.act_weightlist;
    }

    @Override
    protected void initView() {
        initToolbar(mToolbar, getString(R.string.guobandanluru), new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        rfl_weightlist.setEnabled(false);
    }

    @Override
    protected void initData() {
        list = new ArrayList<>();
    }

    @Override
    protected void initOper() {
//        rfl_weightlist.setOnLoadListener(this);
        lv_infos_weight.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
//        page = 0;
        WeightAppList(0,null);
    }

    private void WeightAppList(final int flag,final Date choseTime) {
        ViewOnClickTask WeightAppListTask = new ViewOnClickTask(this,"加载中...") {
            List<RpcObject> rpcList;
            @Override
            protected void successUI() {
                for (int i=0;i<rpcList.size();i++){
                    list.add(new WeightBean(rpcList.get(i).getLong("ID")
                            ,rpcList.get(i).getString("BreedFactory_Name")
                            ,rpcList.get(i).getString("Car_Name"),rpcList.get(i).getInt("CarNumber")));
                }
                adapter = new WeightlistAdapter(WeightListActivity.this,list);
                lv_infos_weight.setAdapter(adapter);
            }

            @Override
            public Object call() throws Exception {
                RpcObject queryObj = RpcObject.create(RpcUrl.WeightAppList_canshu);
                queryObj.setInt("PageSize",100);
                queryObj.setInt("PageIndex",0);
                queryObj.setBoolean("OnlySelfCar",true);
                if (flag ==1) {
                    queryObj.setDate("Date", choseTime);
                }
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.WeightAppList,queryObj);
                rpcList = result.getRpcObjectList(RpcUrl.WeightAppList_fanhui);
                return null;
            }
        };
        WeightAppListTask.execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.chosetime, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.chose_time:
                new CalendarDialogFragment().show(getSupportFragmentManager(), "start");
                break;

        }
        return super.onOptionsItemSelected(item);
    }
//    @Override
//    public void onLoad() {
//        rfl_weightlist.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                rfl_weightlist.setLoading(false);
//                for (int i=0;i<5;i++){
//                    list.add(new WeightBean(i,"场"+i,"che"+i,i+12));
//                }
//                adapter.notifyDataSetChanged();
//            }
//        },1000);
//    }

    @Override
    public void onStartCalendarClick(Date date) {
        WeightAppList(1,date);
    }

    @Override
    public void onEndCalendarClick(Date date) {

    }
}
