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

import android.graphics.Color;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.sbxdjgl.SqdActivity;
import com.example.administrator.zhapp.adapter.MyFragmentAdapter;
import com.example.administrator.zhapp.entity.TestZkdInfo;
import com.example.administrator.zhapp.fragment.BaseFragment;
import com.example.administrator.zhapp.weight.AutoRadioGroup;
import com.example.administrator.zhapp.weight.NoscrollViewPager;
import java.util.ArrayList;
import java.util.List;

/**
 * 设备巡点检管理-签点
 */
public class SqdFragment extends BaseFragment {

    NoscrollViewPager vp;
    RadioButton rb1;
    RadioButton rb2;
    RadioButton rb3;
    RadioButton rb4;
    AutoRadioGroup rg;
    Button btn_last;
    Button btn_next;


    private SqdActivity mActivity;

    private List<Fragment> list;
    private MyFragmentAdapter adapter;

    private QiandFragment qiand;
    private QianDjlFragment qiandjl;
    private BiaozFragment bz;
    private BeizFragment beiz;

    private int item = 0;
    private List<TestZkdInfo> lists;
    private boolean isEdit = false;

    public SqdFragment() {
    }

    public static SqdFragment newInstance(boolean b, ArrayList<TestZkdInfo> lists) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("edit", b);
        bundle.putParcelableArrayList("list", lists);
        SqdFragment fragment = new SqdFragment();
        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");
        }
    }


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

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

        btn_last.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                item--;
                if (item == -1) {
                    item++;
                    showToast("已经到头了");
                } else {
                    qiand.updata(lists.get(item), item + 1, lists.size());
                    beiz.updata(lists.get(item).getBeiz());
                    bz.update(lists.get(item).getBiaoz());
                }
            }
        });

        btn_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                item++;
                if (item == lists.size()) {
                    item--;
                    showToast("你已经完成任务了");
                } else {
                    qiand.updata(lists.get(item), item + 1, lists.size());
                    beiz.updata(lists.get(item).getBeiz());
                    bz.update(lists.get(item).getBiaoz());
                }
            }
        });
    }

    public void initData() {

        list = new ArrayList<>();
        qiand = QiandFragment.newInstance(isEdit, lists.get(0), lists.size());
        qiandjl = QianDjlFragment.newInstance();
        bz = BiaozFragment.newInstance(isEdit, lists.get(0).getBiaoz());
        beiz = BeizFragment.newInstance(isEdit, lists.get(0).getBeiz());
        list.add(qiand);
        list.add(bz);
        list.add(beiz);
        list.add(qiandjl);
        adapter = new MyFragmentAdapter(getChildFragmentManager(), list);
        vp.setAdapter(adapter);
        vp.setOffscreenPageLimit(4);
        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);
                    rb4.setTextColor(Color.GRAY);
                    vp.setCurrentItem(0, false);
                }
                if (i == R.id.rb2) {
                    rb1.setTextColor(Color.GRAY);
                    rb2.setTextColor(Color.WHITE);
                    rb3.setTextColor(Color.GRAY);
                    rb4.setTextColor(Color.GRAY);
                    vp.setCurrentItem(1, false);
                }
                if (i == R.id.rb3) {
                    rb1.setTextColor(Color.GRAY);
                    rb2.setTextColor(Color.GRAY);
                    rb3.setTextColor(Color.WHITE);
                    rb4.setTextColor(Color.GRAY);
                    vp.setCurrentItem(2, false);
                }
                if (i == R.id.rb4) {
                    rb1.setTextColor(Color.GRAY);
                    rb2.setTextColor(Color.GRAY);
                    rb3.setTextColor(Color.GRAY);
                    rb4.setTextColor(Color.WHITE);
                    vp.setCurrentItem(3, false);
                }
            }
        });
    }
}
