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

import android.graphics.Color;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
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.ScjsjActivity;
import com.example.administrator.zhapp.adapter.MyFragmentAdapter;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.DjjhInfo;
import com.example.administrator.zhapp.fragment.BaseFragment;
import com.example.administrator.zhapp.fragment.ajhxcgl.QxgdFragment;
import com.example.administrator.zhapp.fragment.ajhxcgl.SjcjFragment;
import com.example.administrator.zhapp.fragment.ajhxcgl.XcjsFragment;
import com.example.administrator.zhapp.weight.AutoRadioGroup;
import java.util.ArrayList;
import java.util.List;

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

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

    private ScjsjActivity mActivity;
    private MyFragmentAdapter adapter;
    private List<Fragment> list;
    private SjcjFragment sjcj;
    private XcjsFragment xcjs;
    private QxgdFragment qxgd;
    private ArrayList<DjjhRwQy> lists;
    private boolean isEdit = true;
    private int item;

    public SsbdjFragment() {
    }

    public static SsbdjFragment newInstance(boolean b, ArrayList<DjjhInfo.Djjh> lists, int item) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("edit", b);
        bundle.putParcelableArrayList("list", lists);
        bundle.putInt(Contans.KEY_ITEM, item);
        SsbdjFragment fragment = new SsbdjFragment();
        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");
            item = bundle.getInt(Contans.KEY_ITEM);
        }
    }

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

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

    public void initData() {

        list = new ArrayList<>();
        sjcj = SjcjFragment.newInstance(isEdit, lists, item);
        xcjs = XcjsFragment.newInstance(isEdit, Contans.SDLBACT);
        qxgd = QxgdFragment.newInstance(isEdit);
        list.add(sjcj);
        list.add(xcjs);
        list.add(qxgd);
        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);
                                              }
                                              if (i == R.id.rb2) {
                                                  rb1.setTextColor(Color.GRAY);
                                                  rb2.setTextColor(Color.WHITE);
                                                  rb3.setTextColor(Color.GRAY);
                                                  vp.setCurrentItem(1, false);
                                              }
                                              if (i == R.id.rb3) {
                                                  rb1.setTextColor(Color.GRAY);
                                                  rb2.setTextColor(Color.GRAY);
                                                  rb3.setTextColor(Color.WHITE);
                                                  vp.setCurrentItem(2, false);
                                              }
                                          }
                                      }

        );

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {

                switch (position) {
                    case 0:
                        rb1.setChecked(true);
                        rb1.setTextColor(getResources().getColor(R.color.btn));
                        rb2.setTextColor(Color.GRAY);
                        rb3.setTextColor(Color.GRAY);
                        break;
                    case 1:
                        rb2.setChecked(true);
                        rb1.setTextColor(Color.GRAY);
                        rb2.setTextColor(getResources().getColor(R.color.btn));
                        rb3.setTextColor(Color.GRAY);
                        break;
                    case 2:
                        rb3.setChecked(true);
                        rb1.setTextColor(Color.GRAY);
                        rb2.setTextColor(Color.GRAY);
                        rb3.setTextColor(getResources().getColor(R.color.btn));
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    /**
     * 给现场记事传递计划id和poinnum
     */
    public void setXcjs() {
        if (lists.size() != 0) {
            int item = sjcj.getItem();
            xcjs.setJhPoin(lists.get(item).getJHID(), lists.get(item).getPOINTNUM());
        }
    }

    public void sjcjNext() {
        sjcj.next();
    }
}
