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

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.adapter.MyFragmentAdapter;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.fragment.BaseFragment;
import com.example.administrator.zhapp.weight.AutoRadioGroup;

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


/**
 * 安健环-设备点检
 */
public class SbdjFragment extends BaseFragment {

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

    private MyFragmentAdapter adapter;
    private List<Fragment> list;
    private SjcjFragment sjcj;
    private XcjsFragment xcjs;
    private QxgdFragment qxgd;
    private boolean isEdit = false;

    private ArrayList<DjjhRwQy> lists;

    public SbdjFragment() {
    }

    public static SbdjFragment newInstance(boolean b, ArrayList<DjjhRwQy> list) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("edit", b);
        bundle.putParcelableArrayList(Contans.KEY_DJJHLIST, list);
        SbdjFragment fragment = new SbdjFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            visible();
        }
    }

    //显示
    private void visible() {
        //initData();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            isEdit = bundle.getBoolean("edit");
            lists = bundle.getParcelableArrayList(Contans.KEY_DJJHLIST);
        }
    }

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

    @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);
    }

    public void initData() {
        list = new ArrayList<>();

        //if (lists.size() != 0) {
        sjcj = SjcjFragment.newInstance(isEdit, lists, 0);
        xcjs = XcjsFragment.newInstance(isEdit, Contans.AjhACT);
        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) {
                                              switch (i) {
                                                  case R.id.rb1:
                                                      rb1.setTextColor(Color.WHITE);
                                                      rb2.setTextColor(Color.GRAY);
                                                      rb3.setTextColor(Color.GRAY);
                                                      vp.setCurrentItem(0, false);
                                                      break;
                                                  case R.id.rb2:
                                                      rb1.setTextColor(Color.GRAY);
                                                      rb2.setTextColor(Color.WHITE);
                                                      rb3.setTextColor(Color.GRAY);
                                                      vp.setCurrentItem(1, false);
                                                      break;
                                                  case R.id.rb3:
                                                      rb1.setTextColor(Color.GRAY);
                                                      rb2.setTextColor(Color.GRAY);
                                                      rb3.setTextColor(Color.WHITE);
                                                      vp.setCurrentItem(2, false);
                                                      break;
                                              }
                                          }
                                      }
        );

        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 setLists(ArrayList<DjjhRwQy> list) {
        this.lists = list;
        if (lists.size() != 0) {
            if (sjcj != null) {
                sjcj.updata(lists);
            }
        }
    }

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

}
