package com.prankapp.fartsounds.airhorn;

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

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.prankapp.fartsounds.airhorn.adapterdfds.MyFragmentPagerAdapter;
import com.prankapp.fartsounds.airhorn.ui.HomeFragment;
import com.prankapp.fartsounds.airhorn.ui.MineFragment;
import com.prankapp.fartsounds.airhorn.utilsfff.ImmUtils;
import com.prankapp.fartsounds.airhorn.view.NoScrollViewPager;

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

public class MainActivity extends AppCompatActivity {
    List<Fragment> fragmentList = new ArrayList<>();
    RadioGroup radioGroup;
    RadioButton radio_home;
    RadioButton radio_mine;
    NoScrollViewPager noScrollViewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        noScrollViewPager = findViewById(R.id.viewpager);
        radioGroup = findViewById(R.id.radioGroup);
        radio_home = findViewById(R.id.radio_home);
        radio_mine = findViewById(R.id.radio_mine);


        ImmUtils.setStatusBar(this,false,false);
        fragmentList.clear();

        HomeFragment homeFragment = new HomeFragment();
        MineFragment mineFragment = new MineFragment();
        fragmentList.add(homeFragment);
        fragmentList.add(mineFragment);

        noScrollViewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
        noScrollViewPager.setCurrentItem(0);
        noScrollViewPager.setNoScroll(true);
        noScrollViewPager.setOffscreenPageLimit(3);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                if(R.id.radio_home == checkedId){
                    noScrollViewPager.setCurrentItem(0, false);
                }

                if(R.id.radio_mine == checkedId){
                    noScrollViewPager.setCurrentItem(1, false);
                }

            }
        });

        Drawable drawable1=getResources().getDrawable(R.drawable.selector_home);
        drawable1.setBounds(0,0,dip2px(this,20),dip2px(this,20));
        radio_home.setCompoundDrawables(null,drawable1,null,null);

        Drawable drawable2=getResources().getDrawable(R.drawable.selector_my);
        drawable2.setBounds(0,0,dip2px(this,20),dip2px(this,20));
        radio_mine.setCompoundDrawables(null,drawable2,null,null);


    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}