package com.example.administrator.zhapp.fragment.ajhxcgl;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.appcompat.app.AlertDialog;

import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.administrator.zhapp.DBModel.DjjhRwQy;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.sbxdjgl.SdlbActivity;
import com.example.administrator.zhapp.adapter.MyFragmentAdapter;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.fragment.BaseFragment;
import com.example.administrator.zhapp.utils.ControllerActivity;
import com.example.administrator.zhapp.weight.AutoRadioGroup;
import com.example.administrator.zhapp.weight.NoscrollViewPager;

import org.litepal.crud.DataSupport;

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


/**
 * 安健环巡查管理-数据采集
 */
public class SjcjFragment extends BaseFragment {

    NoscrollViewPager vp;
    RadioButton rb1;
    RadioButton rb2;
    RadioButton rb3;
    AutoRadioGroup rg;

    Button btn_last;
    Button btn_next;
    Button btn_exit;
    Button btn_save_next;

    private int checkItem = 0;

    private MyFragmentAdapter adapter;
    private List<Fragment> list;
    private CJFragment cj;
    private FfFragment ff;
    private BzFragment bz;
    private ArrayList<DjjhRwQy> lists;
    private int item = 0;
    private boolean isEdit = false;

    private int index = 0;

    public SjcjFragment() {
    }

    public static SjcjFragment newInstance(boolean b, ArrayList<DjjhRwQy> lists, int item) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("edit", b);
        bundle.putParcelableArrayList("list", lists);
        bundle.putInt(Contans.KEY_ITEM, item);
        SjcjFragment fragment = new SjcjFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            isEdit = bundle.getBoolean("edit");
            lists = bundle.getParcelableArrayList("list");
            index = bundle.getInt(Contans.KEY_ITEM);
            item = bundle.getInt(Contans.KEY_ITEM) + 1;
        }
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_scjsj;
    }

    @Override
    protected void initView() {
        rb1 = view.findViewById(R.id.rb1);
        rb2 = view.findViewById(R.id.rb2);
        rb3 = view.findViewById(R.id.rb3);
        rg = view.findViewById(R.id.rg);
        vp = view.findViewById(R.id.vp);

        btn_last = view.findViewById(R.id.btn_last);
        btn_next = view.findViewById(R.id.btn_next);
        btn_exit = view.findViewById(R.id.btn_exit);
        btn_save_next = view.findViewById(R.id.btn_save_next);

        btn_last.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                --item;
                if (item == 0) {
                    btn_last.setEnabled(false);
                    item++;
                    //showToast("已经到头了");
                } else {
                    cj.updata(lists.get(item - 1), item, lists.size());
                    bz.updata(lists.get(item - 1).getMEASTANDARD());
                    ff.update(lists.get(item - 1).getMEAMETHOD());
                }
            }
        });

        btn_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                next();
            }
        });

        btn_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isEdit) {

                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setTitle("提示");
                    builder.setMessage("你确定要退出吗?");
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    });
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            isEdit = true;

                            //点击退出，处理退出的逻辑
                            if (lists.size() != 0) {
                                if (cj.getRB1()) {//运行中被选中
                                    if (!cj.getCJJG().equals("")) {
                                        ContentValues values = new ContentValues();
                                        values.put("checked", true);
                                        values.put("CJJG", cj.getCJJG());
                                        values.put("fxnr", cj.getFxnr());
                                        values.put("byzt", cj.getRB3() ? 1 : 0);
                                        values.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                                        //更新数据库数据
                                        int i = DataSupport.updateAll(DjjhRwQy.class, values, "POINTNUM = ? and jhid = ? ", lists.get(item - 1).getPOINTNUM(), lists.get(item - 1).getJHID());
                                        if (i != 0) {
                                            showToast("保存成功");
                                        }
                                    } else {
                                        showToast("你没有数据采集结果");
                                    }
                                } else {//停用被选中
                                    List<DjjhRwQy> rwqys = new ArrayList<>();
                                    //更新所有被停用的机器
                                    for (DjjhRwQy qy : lists) {
                                        if (qy.getASSETNUM().equals(lists.get(item - 1).getASSETNUM())) {
                                            qy.setSBZT(false);
                                            qy.setChecked(true);
                                            rwqys.add(qy);
                                        }
                                    }
                                    ContentValues values = new ContentValues();
                                    values.put("checked", true);
                                    //values.put("CJJG", cj.getCJJG());
                                    //values.put("fxnr", cj.getFxnr());
                                    values.put("byzt", 0);
                                    values.put("sbzt", 0);
                                    values.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                                    //更新数据库数据
                                    int i = DataSupport.updateAll(DjjhRwQy.class, values, "jhid = ? and assetnum = ?", lists.get(item - 1).getJHID(), lists.get(item - 1).getASSETNUM());
                                    if (i != 0) {
                                        showToast("保存成功");
                                    }
                                }
                            }
                            ControllerActivity.getAppManager().finishActivity(SdlbActivity.class);
                            getActivity().finish();
                        }
                    });
                    builder.create().show();
                } else {
                    getActivity().finish();
                }
            }
        });
    }

    public void initData() {

        if (!isEdit) {
            btn_save_next.setVisibility(View.GONE);
        }
        list = new ArrayList<>();
        if (lists.size() != 0) {
            cj = CJFragment.newInstance(isEdit, lists.get(index), lists.size(), index + 1);
            ff = FfFragment.newInstance(isEdit, lists.get(index).getMEAMETHOD());
            bz = BzFragment.newInstance(isEdit, lists.get(index).getMEASTANDARD());
        } else {
            cj = CJFragment.newInstance(isEdit, new DjjhRwQy(), 0, 0);
            ff = FfFragment.newInstance(isEdit, "");
            bz = BzFragment.newInstance(isEdit, "");
        }
        list.add(cj);
        list.add(ff);
        list.add(bz);
        adapter = new MyFragmentAdapter(getChildFragmentManager(), list);
        vp.setAdapter(adapter);
        vp.setOffscreenPageLimit(3);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {

                if (i == R.id.rb1) {
                    rb1.setTextColor(Color.WHITE);
                    rb2.setTextColor(Color.GRAY);
                    rb3.setTextColor(Color.GRAY);
                    vp.setCurrentItem(0, false);
                } else if (i == R.id.rb2) {
                    rb1.setTextColor(Color.GRAY);
                    rb2.setTextColor(Color.WHITE);
                    rb3.setTextColor(Color.GRAY);
                    vp.setCurrentItem(1, false);
                } else if (i == R.id.rb3) {
                    rb1.setTextColor(Color.GRAY);
                    rb2.setTextColor(Color.GRAY);
                    rb3.setTextColor(Color.WHITE);
                    vp.setCurrentItem(2, false);
                }
            }
        });
    }

    public void next() {
        if (lists.size() != 0) {
            if (!btn_last.isEnabled()) {
                btn_last.setEnabled(true);
            }
            if (isEdit) {
                if (cj.getRB1()) {  //运行中被选中
                    if (!cj.getCJJG().equals("")) {
                        ContentValues values = new ContentValues();
                        values.put("checked", true);
                        values.put("CJJG", cj.getCJJG());
                        values.put("fxnr", cj.getFxnr());
                        values.put("byzt", cj.getRB3() ? 1 : 0);
                        values.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                        //更新数据库数据
                        int i = DataSupport.updateAll(DjjhRwQy.class, values, "POINTNUM = ? and jhid = ? ", lists.get(item - 1).getPOINTNUM(), lists.get(item - 1).getJHID());
                        if (i != 0) {
                            lists.get(item - 1).setChecked(true);
                            lists.get(item - 1).setCJJG(cj.getCJJG());
                            lists.get(item - 1).setFxnr(cj.getFxnr());
                            lists.get(item - 1).setBYZT(cj.getRB3());
                            showToast("保存成功");
                        }
                    } else {
                        showToast("你没有数据采集结果");
                    }
                } else {   //停用被选中
                    List<DjjhRwQy> rwqys = new ArrayList<>();
                    //更新所有被停用的机器
                    for (DjjhRwQy qy : lists) {
                        if (qy.getASSETNUM().equals(lists.get(item - 1).getASSETNUM())) {
                            qy.setSBZT(false);
                            qy.setChecked(true);
                            rwqys.add(qy);
                        }
                    }

                    ContentValues values = new ContentValues();
                    values.put("checked", true);
                    //values.put("CJJG", cj.getCJJG());
                    //values.put("fxnr", cj.getFxnr());
                    values.put("byzt", 0);
                    values.put("sbzt", 0);
                    values.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                    //更新数据库数据
                    int i = DataSupport.updateAll(DjjhRwQy.class, values, "jhid = ? and assetnum = ?", lists.get(item - 1).getJHID(), lists.get(item - 1).getASSETNUM());

                    if (i != 0) {
//                                item = i + item + 1;
                        if (lists.size() != rwqys.size()) {
                            lists.removeAll(rwqys);

                            int check = 0;
                            for (int j = 0; j < lists.size(); j++) {
                                if (lists.get(j).isChecked()) {
                                    check++;
                                }
                            }
                            item = check;
                            showToast("保存成功");
                        } else {
                            item = lists.size();
                            lists.get(item - 1).setChecked(true);
                            lists.get(item - 1).setCJJG(cj.getCJJG());
                            lists.get(item - 1).setFxnr(cj.getFxnr());
                        }
                    }
                }
                ++item;
                if (item > lists.size()) {
                    item--;
                    cj.updata(lists.get(item - 1), item, lists.size());
                    //showToast("到底了");
                } else {
                    cj.updata(lists.get(item - 1), item, lists.size());
                    bz.updata(lists.get(item - 1).getMEASTANDARD());
                    ff.update(lists.get(item - 1).getMEAMETHOD());
                }
            } else {
                if (lists.size() != 0) {
                    ++item;
                    if (!btn_last.isEnabled()) {
                        btn_last.setEnabled(true);
                    }
                    if (item > lists.size()) {
                        item--;
                        //showToast("到底了");
                    } else {
                        cj.updata(lists.get(item - 1), item, lists.size());
                        bz.updata(lists.get(item - 1).getMEASTANDARD());
                        ff.update(lists.get(item - 1).getMEAMETHOD());
                    }
                }
            }
        }
    }

    public int getItem() {
        return item - 1;
    }

    public void updata(List<DjjhRwQy> lists) {
        cj.updata(lists.get(0), 1, lists.size());
        bz.updata(lists.get(0).getMEASTANDARD());
        ff.update(lists.get(0).getMEAMETHOD());
    }
}