package com.rehome.sgbaxj.activity;

import android.graphics.Color;
import android.widget.RadioButton;
import com.rehome.sgbaxj.R;
import com.rehome.sgbaxj.base.BaseActivity;
import com.rehome.sgbaxj.utils.AutoToolbar;
import com.rehome.sgbaxj.utils.CanBanScrollViewPager;
import com.rehome.sgbaxj.weight.AutoRadioGroup;

import java.util.ArrayList;
import java.util.List;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

/**
 * Created by ruihong on 2018/4/2.
 */

public class TabMainActivity extends BaseActivity {

    CanBanScrollViewPager vp;
    RadioButton rbHome;
    RadioButton rbContact;
    RadioButton rbMine;
    AutoRadioGroup rg;
    AutoToolbar toolbar;

    private List<Fragment> fragments = new ArrayList<>();
    private FragmentPagerAdapter adapter;
//    BadgeView badgeView_me;

    @Override
    public int getContentViewID() {
        return R.layout.activity_tabmain;
    }

    @Override
    protected void initView() {

        vp = findViewById(R.id.vp);
        rbHome = findViewById(R.id.rb_home);
        rbContact = findViewById(R.id.rb_contact);
        rbMine = findViewById(R.id.rb_mine);
        rg = findViewById(R.id.rg);
        toolbar = findViewById(R.id.toolbar);


        fragments.add(MainFragment.getInstance());
        fragments.add(ContactFragment.getInstance());
        fragments.add(MineFragment.getInstance());


        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        };


        vp.setAdapter(adapter);
        vp.setOffscreenPageLimit(3);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                initTextColor();

                switch (position) {
                    case 0:
                        title.setText("管理");
                        vp.setCurrentItem(0, false);
                        rbHome.setTextColor(Color.parseColor("#0099ff"));
                        rbHome.setChecked(true);
                        break;
                    case 1:
                        title.setText("通讯录");
                        vp.setCurrentItem(1, false);
                        rbContact.setTextColor(Color.parseColor("#0099ff"));
                        rbContact.setChecked(true);
                        break;
                    case 2:
                        title.setText("我的");
                        vp.setCurrentItem(2, false);
                        rbMine.setTextColor(Color.parseColor("#0099ff"));
                        rbMine.setChecked(true);
                        break;
                }


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        rg.setBackgroundColor(getResources().getColor(R.color.white));

        rg.setOnCheckedChangeListener((radioGroup, i) -> {
            initTextColor();
            switch (i) {
                case R.id.rb_home:
                    title.setText("运行");
                    vp.setCurrentItem(0, false);
                    rbHome.setTextColor(Color.parseColor("#0099ff"));
                    break;
                case R.id.rb_contact:
                    title.setText("通讯录");
                    vp.setCurrentItem(1, false);
                    rbContact.setTextColor(Color.parseColor("#0099ff"));
                    break;
                case R.id.rb_mine:
                    title.setText("我的");
                    vp.setCurrentItem(2, false);
                    rbMine.setTextColor(Color.parseColor("#0099ff"));
                    break;
            }
        });
    }
    private void initTextColor() {
        rbHome.setTextColor(Color.parseColor("#abadbb"));
        rbContact.setTextColor(Color.parseColor("#abadbb"));
        rbMine.setTextColor(Color.parseColor("#abadbb"));
    }


    public void initData() {
        title.setText("首页");
    }

    @Override
    protected void onResume() {
        super.onResume();
//        badgeView_me.setBadgeCount(zjrwNum);
    }
    @Override
    protected void onRestart() {
        super.onRestart();
//        badgeView_me.setBadgeCount(zjrwNum);
    }
}
