package com.qhclh.ytzh.work.tablepoultry;

import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AbsListView;
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.ui.LinkedHorizontalScrollView;
import com.qhclh.ytzh.ui.NoScrollHorizontalScrollView;

import java.util.ArrayList;
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<String> 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);
    }

    @Override
    protected void initData() {
        buildhouseList = new ArrayList<>();
        reportBeanList = new ArrayList<>();

        for (int i=0;i<10;i++){
            buildhouseList.add("栋舍"+i);
            reportBeanList.add(new ReportBean("1"+i,"2"+i,"3"+i,"4"+i,"5"+i));
        }

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

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

    }

    @Override
    protected void initOper() {

    }


    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());
                }
            }
        });
    }
}
