package com.phlx.anchorcollect.ui.info;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.lingber.mycontrol.datagridview.DataGridView;
import com.lingber.mycontrol.datagridview.RecyclerviewAdapter;
import com.phlx.anchorcollect.BR;
import com.phlx.anchorcollect.Configs;
import com.phlx.anchorcollect.R;
import com.phlx.anchorcollect.data.VMFactory;
import com.phlx.anchorcollect.databinding.AvtivityCattleBinding;
import com.phlx.anchorcollect.entity.BreedingRecordDetailEntity;
import com.phlx.anchorcollect.entity.BreedingRecordEntity;
import com.phlx.anchorcollect.entity.GenTableColumn;
import com.phlx.anchorcollect.entity.ImmuneBatchEntity;
import com.phlx.anchorcollect.entity.MedicalRecordEntity;
import com.phlx.anchorcollect.entity.PerformanceEntity;
import com.phlx.anchorcollect.ui.base.BaseActivity;
import com.phlx.anchorcollect.ui.main.MainActivity;
import com.phlx.anchorcollect.util.StringUtil;

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


public class CattleActivity extends BaseActivity<AvtivityCattleBinding, CattleVM> {


    private DataGridView refreshDataGridView;


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.avtivity_cattle;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
        super.initParam();
//        Bundle mBundle = getIntent().getExtras();
//        if (mBundle != null) {
//            unid = mBundle.getString("unid");
//        }
    }

    @Override
    public CattleVM initViewModel() {
        VMFactory factory = VMFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(CattleVM.class);
    }

    @Override
    public void initData() {
        super.initData();
        //初始化标题
        viewModel.initToolbar();

        initSignMeasureGrid();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.observableRefreshList.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                updateRecordData(integer);
            }
        });
    }

    //性能测定
    private void initSignMeasureGrid() {

        DataGridView mDataGridView = binding.dgvSignMeasure;

        //初始化表头
        List<String> headerStr = new ArrayList<>();
        List<String> fieldNameStr = new ArrayList<>();
        for (GenTableColumn gtc : Configs.performance) {
            if ("1".equals(gtc.getIsList())) {
                headerStr.add(gtc.getColumnComment());
                fieldNameStr.add(gtc.getJavaField());
            }
        }

//        // 设置列数
        mDataGridView.setColunms(headerStr.size());
//        // 设置表头内容
//        mDataGridView.setHeaderHeight(30);
        mDataGridView.setHeaderContent(StringUtil.listToStr(headerStr));
//        // 绑定字段
        mDataGridView.setFieldNames(StringUtil.listToStr(fieldNameStr));
//        // 每个column占比
        mDataGridView.setColunmWeight(StringUtil.getFloats(headerStr.size()));
//        // 每个单元格包含控件
        mDataGridView.setCellContentView(StringUtil.getClasses(headerStr.size()));

        // 单行选中模式
        mDataGridView.setSelectedMode(1);

        // 设置列数
//        mDataGridView.setColunms(3);
        // 设置行高
        mDataGridView.setRowHeight(100);

        // 启用翻页
//        mDataGridView.setFlipOverEnable(true, 9, getFragmentManager());

        // 初始化表格
        mDataGridView.initDataGridView();

        mDataGridView.setOnItemCellClickListener(new RecyclerviewAdapter.OnItemCellClickListener() {
            @Override
            public void onClick(View v, int row, int column) {
//                ToastUtils.showShort("row:" + row + "     column:" + column);
                PerformanceEntity rowData = (PerformanceEntity) (mDataGridView.getRowData(row));

                new MaterialDialog
                        .Builder(CattleActivity.this)
                        .title("替换提示")
                        .content("要跳转性能测定详情吗?")
                        .positiveText("确定")
                        .negativeText("取消")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                if (which == DialogAction.POSITIVE) {
                                    Configs.mainPosition = 1;
                                    Configs.resumeId = rowData.getUnid();
                                    startActivity(MainActivity.class);
                                    finish();
                                }
                            }
                        }).show();
            }
        });

        viewModel.querySignMeasureList();
    }

    //诊疗记录
    private void initMedicalRecordGrid() {

        DataGridView mDataGridView = binding.dgvMedicalRecord;

        //初始化表头
        List<String> headerStr = new ArrayList<>();
        List<String> fieldNameStr = new ArrayList<>();
        for (GenTableColumn gtc : Configs.medicalrecords) {
            if ("1".equals(gtc.getIsList())) {
                headerStr.add(gtc.getColumnComment());
                fieldNameStr.add(gtc.getJavaField());
            }
        }

//        // 设置列数
        mDataGridView.setColunms(headerStr.size());
//        // 设置表头内容
//        mDataGridView.setHeaderHeight(30);
        mDataGridView.setHeaderContent(StringUtil.listToStr(headerStr));
//        // 绑定字段
        mDataGridView.setFieldNames(StringUtil.listToStr(fieldNameStr));
//        // 每个column占比
        mDataGridView.setColunmWeight(StringUtil.getFloats(headerStr.size()));
//        // 每个单元格包含控件
        mDataGridView.setCellContentView(StringUtil.getClasses(headerStr.size()));

        // 单行选中模式
        mDataGridView.setSelectedMode(1);

        // 设置列数
//        mDataGridView.setColunms(3);
        // 设置行高
        mDataGridView.setRowHeight(100);

        // 启用翻页
//        mDataGridView.setFlipOverEnable(true, 9, getFragmentManager());

        // 初始化表格
        mDataGridView.initDataGridView();

        mDataGridView.setOnItemCellClickListener(new RecyclerviewAdapter.OnItemCellClickListener() {
            @Override
            public void onClick(View v, int row, int column) {
//                ToastUtils.showShort("row:" + row + "     column:" + column);
                MedicalRecordEntity rowData = (MedicalRecordEntity) (mDataGridView.getRowData(row));

                new MaterialDialog
                        .Builder(CattleActivity.this)
                        .title("替换提示")
                        .content("要跳转诊疗记录详情吗?")
                        .positiveText("确定")
                        .negativeText("取消")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                if (which == DialogAction.POSITIVE) {
                                    Configs.mainPosition = 3;
                                    Configs.resumeId = rowData.getUnid();
                                    startActivity(MainActivity.class);
                                    finish();
                                }
                            }
                        }).show();
            }
        });

        viewModel.queryMedicalRecordList();

    }

    //免疫记录
    private void initImmunRecordGrid() {

        DataGridView mDataGridView = binding.dgvImmunRecord;

        //初始化表头
        List<String> headerStr = new ArrayList<>();
        List<String> fieldNameStr = new ArrayList<>();
        for (GenTableColumn gtc : Configs.immunebatch) {
            if ("1".equals(gtc.getIsList())) {
                headerStr.add(gtc.getColumnComment());
                fieldNameStr.add(gtc.getJavaField());
            }
        }

//        // 设置列数
        mDataGridView.setColunms(headerStr.size());
//        // 设置表头内容
//        mDataGridView.setHeaderHeight(30);
        mDataGridView.setHeaderContent(StringUtil.listToStr(headerStr));
//        // 绑定字段
        mDataGridView.setFieldNames(StringUtil.listToStr(fieldNameStr));
//        // 每个column占比
        mDataGridView.setColunmWeight(StringUtil.getFloats(headerStr.size()));
//        // 每个单元格包含控件
        mDataGridView.setCellContentView(StringUtil.getClasses(headerStr.size()));

        // 单行选中模式
        mDataGridView.setSelectedMode(1);

        // 设置列数
//        mDataGridView.setColunms(3);
        // 设置行高
        mDataGridView.setRowHeight(100);

        // 启用翻页
//        mDataGridView.setFlipOverEnable(true, 9, getFragmentManager());

        // 初始化表格
        mDataGridView.initDataGridView();

        mDataGridView.setOnItemCellClickListener(new RecyclerviewAdapter.OnItemCellClickListener() {
            @Override
            public void onClick(View v, int row, int column) {
//                ToastUtils.showShort("row:" + row + "     column:" + column);
                ImmuneBatchEntity rowData = (ImmuneBatchEntity) (mDataGridView.getRowData(row));

                new MaterialDialog
                        .Builder(CattleActivity.this)
                        .title("替换提示")
                        .content("要跳转免疫记录详情吗?")
                        .positiveText("确定")
                        .negativeText("取消")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                if (which == DialogAction.POSITIVE) {
                                    Configs.mainPosition = 4;
                                    Configs.resumeId = rowData.getUnid();
                                    startActivity(MainActivity.class);
                                    finish();
                                }
                            }
                        }).show();
            }
        });

        viewModel.queryImmunRecordList();

    }

    //配种记录
    private void initBreedingRecordGrid() {

        DataGridView mDataGridView = binding.dgvBreedingRecord;

        //初始化表头
        List<String> headerStr = new ArrayList<>();
        List<String> fieldNameStr = new ArrayList<>();
        for (GenTableColumn gtc : Configs.cattlematingMasterList) {
            if ("1".equals(gtc.getIsList())) {
                headerStr.add(gtc.getColumnComment());
                fieldNameStr.add(gtc.getJavaField());
            }
        }

//        // 设置列数
        mDataGridView.setColunms(headerStr.size());
//        // 设置表头内容
//        mDataGridView.setHeaderHeight(30);
        mDataGridView.setHeaderContent(StringUtil.listToStr(headerStr));
//        // 绑定字段
        mDataGridView.setFieldNames(StringUtil.listToStr(fieldNameStr));
//        // 每个column占比
        mDataGridView.setColunmWeight(StringUtil.getFloats(headerStr.size()));
//        // 每个单元格包含控件
        mDataGridView.setCellContentView(StringUtil.getClasses(headerStr.size()));

        // 单行选中模式
        mDataGridView.setSelectedMode(0);

        // 设置列数
//        mDataGridView.setColunms(3);
        // 设置行高
        mDataGridView.setRowHeight(100);

        // 启用翻页
//        mDataGridView.setFlipOverEnable(true, 9, getFragmentManager());

        // 初始化表格
        mDataGridView.initDataGridView();

        mDataGridView.setOnItemCellClickListener(new RecyclerviewAdapter.OnItemCellClickListener() {
            @Override
            public void onClick(View v, int row, int column) {
//                ToastUtils.showShort("row:" + row + "     column:" + column);
                BreedingRecordDetailEntity rowData = (BreedingRecordDetailEntity) (mDataGridView.getRowData(row));

                new MaterialDialog
                        .Builder(CattleActivity.this)
                        .title("替换提示")
                        .content("要跳转配种数据详情吗?")
                        .positiveText("确定")
                        .negativeText("取消")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                if (which == DialogAction.POSITIVE) {
                                    Configs.mainPosition = 5;
                                    Configs.resumeId = rowData.getFUnid();
                                    startActivity(MainActivity.class);
                                    finish();
                                }
                            }
                        }).show();
            }
        });

        viewModel.queryBreedingRecordDetailList();

    }

    //刷新数据
    private void updateRecordData(int type) {

        List<BreedingRecordDetailEntity> dataSource = new ArrayList<>();
        int refreshType = 0;
        switch (type) {
            case 0:
                refreshDataGridView = binding.dgvSignMeasure;
                refreshDataGridView.setDataSource(viewModel.getPerformanceEntityList());
                initMedicalRecordGrid();
                break;
            case 1:
                refreshDataGridView = binding.dgvMedicalRecord;
                refreshDataGridView.setDataSource(viewModel.getMedicalRecordEntityList());
                initImmunRecordGrid();
                break;
            case 2:
                refreshDataGridView = binding.dgvImmunRecord;
                refreshDataGridView.setDataSource(viewModel.getImmuneBatchEntityList());
                initBreedingRecordGrid();
                break;
            case 3:
                refreshDataGridView = binding.dgvBreedingRecord;
                refreshDataGridView.setDataSource(viewModel.getBreedingRecordEntityList());
                break;
        }

        refreshDataGridView.updateAll();

    }


}
