package com.qhclh.ytzh.work.Poultrydailyreport;

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.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.tablepoultry.LvBuildhousenaneAdapter;

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

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

import butterknife.BindView;

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

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

    private boolean isLeftListEnabled = false;
    private boolean isRightListEnabled = false;

    private LvBuildhousenaneAdapter lvBuildhousenaneAdapter;
    private PoultryReportChandanInfoAdapter poultryReportInfoAdapter;

    private List<ReportString> buildhouseList;
    private List<PoultryReportBean> reportBeanList;

    private long BreedFactory_ID;
    private long time;
    @Override
    protected int setLayoutId() {
        return R.layout.act_poultry_chandan_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() {
        BreedFactory_ID = getIntent().getLongExtra("BreedFactory_ID",-1);
        time = getIntent().getLongExtra("time",0);
        buildhouseList = new ArrayList<>();
        reportBeanList = new ArrayList<>();
    }

    @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(PoultryReportChandanActvity.this,PoultryDataChartActivity.class);
                intent.putExtra("House_ID",l);
                intent.putExtra("House_Name",reportBeanList.get(i).getHouse_Name());
                intent.putExtra("TYPE",2);
                intent.putExtra("tiem",time);
                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(PoultryReportChandanActvity.this,PoultryDataChartActivity.class);
                intent.putExtra("House_ID",l);
                intent.putExtra("House_Name",reportBeanList.get(i).getHouse_Name());
                intent.putExtra("TYPE",2);
                intent.putExtra("tiem",time);
                startActivity(intent);
            }
        });
    }

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

    private void HouseTotalInfoCount2() {
        ViewOnClickTask HouseTotalInfoCount2Task = new ViewOnClickTask(this,"加载中...") {
            List<RpcObject> rpcList;
            @Override
            protected void successUI() {
                buildhouseList.clear();
                reportBeanList.clear();
                for (int i = 0;i<rpcList.size();i++){
                    String House_Name="",Week="",FemaleSiTaoNumber="",MaleSiTaoNumber=""
                            ,danzhong="",chandanlv="",chandanlvfudong="",FemalePerFeedNumber="",FemaleEatTime="";
                    if (rpcList.get(i).getString("House_Name")!=null){
                        House_Name = rpcList.get(i).getString("House_Name");
                    }
                    if (rpcList.get(i).getString("Week")!=null){
                        Week = rpcList.get(i).getString("Week");
                    }
                    if (rpcList.get(i).getInt("FemaleDaySiTaoNumber")!=null){
                        FemaleSiTaoNumber = rpcList.get(i).getInt("FemaleDaySiTaoNumber")+"";
                    }
                    if (rpcList.get(i).getInt("MaleDaySiTaoNumber")!=null){
                        MaleSiTaoNumber = rpcList.get(i).getInt("MaleDaySiTaoNumber")+"";
                    }
                    if (rpcList.get(i).getDecimal("AvgEggWeight")!=null){
                        danzhong = rpcList.get(i).getDecimal("AvgEggWeight")+"";
                    }
                    if (rpcList.get(i).getDecimal("EggDayRate")!=null){
                        chandanlv = rpcList.get(i).getDecimal("EggDayRate").multiply(new BigDecimal("100")).stripTrailingZeros().toPlainString()+"%";
                    }
                    if (rpcList.get(i).getDecimal("EggDayRateFloat")!=null){
                        chandanlvfudong = rpcList.get(i).getDecimal("EggDayRateFloat").multiply(new BigDecimal("100")).stripTrailingZeros().toPlainString()+"%";
                    }
                    if (rpcList.get(i).getDecimal("FemalePerFeedNumber")!=null){
                        FemalePerFeedNumber = rpcList.get(i).getDecimal("FemalePerFeedNumber")+"";
                    }
                    if (rpcList.get(i).getDecimal("FemaleEatTime")!=null){
                        FemaleEatTime = rpcList.get(i).getDecimal("FemaleEatTime")+"";
                    }
                    buildhouseList.add(new ReportString(rpcList.get(i).getLong("House_ID"),House_Name));
                    reportBeanList.add(new PoultryReportBean(rpcList.get(i).getLong("House_ID"),House_Name,Week,FemaleSiTaoNumber
                            ,MaleSiTaoNumber,FemalePerFeedNumber,FemaleEatTime,danzhong,chandanlv,chandanlvfudong));

                    lvBuildhousenaneAdapter = new LvBuildhousenaneAdapter(PoultryReportChandanActvity.this,buildhouseList);
                    poultryReportInfoAdapter = new PoultryReportChandanInfoAdapter(PoultryReportChandanActvity.this,reportBeanList);

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

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

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