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

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

    private void HouseTotalInfoCount1() {
        ViewOnClickTask HouseTotalInfoCount1Task = 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=""
                            ,FemaleSurvivalRate="",MaleSurvivalRate="",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("FemaleSurvivalRate")!=null){
                        FemaleSurvivalRate = rpcList.get(i).getDecimal("FemaleSurvivalRate").multiply(new BigDecimal("100")).stripTrailingZeros().toPlainString()+"%";
                    }
                    if (rpcList.get(i).getDecimal("MaleSurvivalRate")!=null){
                        MaleSurvivalRate = rpcList.get(i).getDecimal("MaleSurvivalRate").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 && rpcList.get(i).getDecimal("FemaleEatTime").compareTo(BigDecimal.ZERO)!=0){
                        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,FemaleSurvivalRate,MaleSurvivalRate,FemalePerFeedNumber,FemaleEatTime));

                    lvBuildhousenaneAdapter = new LvBuildhousenaneAdapter(PoultryReportActvity.this,buildhouseList);
                    poultryReportInfoAdapter = new PoultryReportInfoAdapter(PoultryReportActvity.this,reportBeanList);

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

            @Override
            public Object call() throws Exception {
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.HouseTotalInfoCount1,new SimpleDateFormat("yyyy-MM-dd").parse(DateTimeUtil.getYMD(new Date(time))),BreedFactory_ID);
                rpcList = result.getRpcObjectList(RpcUrl.HouseTotalInfoCount1_fanhui);
                return null;
            }
        };
        HouseTotalInfoCount1Task.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());
                }
            }
        });
    }
}
