package com.qhclh.ytzh.work.tablepoultry;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.HorizontalScrollView;
import android.widget.ListView;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseActivity;
import com.qhclh.ytzh.bean.ReportBean;
import com.qhclh.ytzh.bean.ReportString;
import com.qhclh.ytzh.home.RpcUrl;
import com.qhclh.ytzh.tasks.ViewOnClickTask;
import com.qhclh.ytzh.ui.LinkedHorizontalScrollView;
import com.qhclh.ytzh.ui.NoScrollHorizontalScrollView;
import com.qhclh.ytzh.utils.DateTimeUtil;
import com.qhclh.ytzh.work.breedpoultry.DatanalysisActivity;

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

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

import butterknife.BindView;

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

public class ReportActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.sv_title)
    NoScrollHorizontalScrollView sv_title;//不可滑动的顶部左侧的ScrollView
    @BindView(R.id.sv_report_detail)
    LinkedHorizontalScrollView sv_report_detail;//底部左侧的ScrollView
    @BindView(R.id.lv_buildhousename)
    ListView lv_buildhousename;//底部左侧的ListView
    @BindView(R.id.lv_report_info)
    ListView lv_report_info;//底部右侧的ListView

    private boolean isLeftListEnabled = false;
    private boolean isRightListEnabled = false;
    private LvBuildhousenaneAdapter lvBuildhousenaneAdapter;
    private LvReportInfoAdapter lvReportInfoAdapter;

    private List<ReportString> buildhouseList;
    private List<ReportBean> reportBeanList;
    @Override
    protected int setLayoutId() {
        return R.layout.act_report;
    }

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

        combination(lv_buildhousename, lv_report_info, sv_title, sv_report_detail);
    }

    private long bfid;
    private long time;
    @Override
    protected void initData() {
        bfid = getIntent().getLongExtra("BreedFactory_id",0);
        time = getIntent().getLongExtra("time",0);
        buildhouseList = new ArrayList<>();
        reportBeanList = new ArrayList<>();

    }

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

    private void DetailListView() {
        ViewOnClickTask DetailListViewTask = new ViewOnClickTask(this,"加载中...") {
            List<RpcObject> rpc;
            @Override
            protected void successUI() {
                buildhouseList.clear();
                reportBeanList.clear();
                for (int i=0;i<rpc.size();i++){
                    buildhouseList.add(new ReportString(rpc.get(i).getLong("ID"),""+(i+1)));
                    String days = "";
                    if (rpc.get(i).getInt("Days")!=null) {
                        days = rpc.get(i).getInt("Days") + "";
                    }
                    String dies = "";
                    if (rpc.get(i).getInt("DieNumber")!=null){
                        dies = rpc.get(i).getInt("DieNumber")+"";
                    }
                    String obs = "";
                    if (rpc.get(i).getInt("Obsolete")!=null){
                        obs = rpc.get(i).getInt("Obsolete")+"";
                    }
                    String water = "";
                    if (rpc.get(i).getDecimal("WaterIntake")!=null){
                        water = rpc.get(i).getDecimal("WaterIntake")+"";
                    }

                    String weight = "";
                    if (rpc.get(i).getDecimal("Weight")!=null){
                        weight = rpc.get(i).getDecimal("Weight")+"";
                    }
                    reportBeanList.add(new ReportBean(rpc.get(i).getLong("ID")
                            ,days
                            ,dies
                            ,obs
                            ,water
                            ,weight
                            ,rpc.get(i).getString("House_Name")));
                }

                lvBuildhousenaneAdapter = new LvBuildhousenaneAdapter(ReportActivity.this,buildhouseList);
                lvReportInfoAdapter = new LvReportInfoAdapter(ReportActivity.this,reportBeanList);

                lv_buildhousename.setAdapter(lvBuildhousenaneAdapter);
                lv_report_info.setAdapter(lvReportInfoAdapter);
            }

            @Override
            public Object call() throws Exception {
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.DetailListView,bfid,new SimpleDateFormat("yyyy-MM-dd").parse(DateTimeUtil.getYMD(new Date(time))));
                rpc = result.getRpcObjectList(RpcUrl.DetailListView_fanhui);
                return null;
            }
        };
        DetailListViewTask.execute();
    }

    @Override
    protected void initOper() {

        lv_buildhousename.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(ReportActivity.this, DatanalysisActivity.class);
                intent.putExtra("id",l);
                startActivity(intent);
            }
        });

        lv_report_info.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(ReportActivity.this, DatanalysisActivity.class);
                intent.putExtra("id",l);
                startActivity(intent);
            }
        });

    }


    private void combination(final ListView lvName, final ListView lvDetail, final HorizontalScrollView title, LinkedHorizontalScrollView content) {
        /**
         * 左右滑动同步
         */
        content.setMyScrollChangeListener(new LinkedHorizontalScrollView.LinkScrollChangeListener() {
            @Override
            public void onscroll(LinkedHorizontalScrollView view, int x, int y, int oldx, int oldy) {
                title.scrollTo(x, y);
            }
        });

        /**
         * 上下滑动同步
         */
        // 禁止快速滑动
        lvName.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        lvDetail.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        //左侧ListView滚动时，控制右侧ListView滚动
        lvName.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                //这两个enable标志位是为了避免死循环
                if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
                    isRightListEnabled = false;
                    isLeftListEnabled = true;
                } else if (scrollState == SCROLL_STATE_IDLE) {
                    isRightListEnabled = true;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                                 int totalItemCount) {
                View child = view.getChildAt(0);
                if (child != null && isLeftListEnabled) {
                    lvDetail.setSelectionFromTop(firstVisibleItem, child.getTop());
                }
            }
        });

        //右侧ListView滚动时，控制左侧ListView滚动
        lvDetail.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
                    isLeftListEnabled = false;
                    isRightListEnabled = true;
                } else if (scrollState == SCROLL_STATE_IDLE) {
                    isLeftListEnabled = true;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                                 int totalItemCount) {
                View c = view.getChildAt(0);
                if (c != null && isRightListEnabled) {
                    lvName.setSelectionFromTop(firstVisibleItem, c.getTop());
                }
            }
        });
    }
}
