package com.example.leudaemialikeme.Activity;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.example.leudaemialikeme.Adapter.APNPageActivityAdapter;
import com.example.leudaemialikeme.R;

public class APNActivity extends BaseActivity implements ViewPager.OnPageChangeListener{
    private static final String ARG_CHANNEL_LIST = "channel_list";
    private View view=null; // 碎片的布局实例
    private ViewPager viewPager; //内导航的碎片的容器
    private RadioGroup rgChannel=null; // 内导航由单选按钮组构成
    private HorizontalScrollView hvChannel=null; //单选按钮组可滚动动
    private ImageView apn_back;
    private String[] channelList = {"回答","帖子"}; //默认的内导航栏目
    private APNPageActivityAdapter adapter; //viewPager 的适配器
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apn);
        viewPager = findViewById(R.id.vpNewsList);
        initViewPager(); //设置 ViewPager
        rgChannel = findViewById(R.id.rgChannel);
        hvChannel = findViewById(R.id.hvChannel);
        initTab(getLayoutInflater());
        rgChannel.setOnCheckedChangeListener(
                new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        viewPager.setCurrentItem(checkedId);
                    }
                }
        );
        apn_back = findViewById(R.id.apn_back);
        apn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void initViewPager() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        adapter = new APNPageActivityAdapter(fragmentManager, channelList);
//参数 channelList 将被适配器用来在动态切换碎片的时候实时创建碎片 //设置 ViewPager 的适配器
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(2);
        //设置显示第 1 个碎片
        viewPager.setCurrentItem(0);
        //设置 ViewPager 的切换监听
        viewPager.addOnPageChangeListener(this);
    }

    private void initTab(LayoutInflater inflater) {
        for(int i=0;i<channelList.length;i++){
            RadioButton rb=(RadioButton)inflater.inflate(R.layout.invitation_tab_rb,null);
            rb.setId(i);
            rb.setText(channelList[i]);
            RadioGroup.LayoutParams params = new
                    RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
                    RadioGroup.LayoutParams.WRAP_CONTENT);
            rgChannel.addView(rb,params);
        }
        rgChannel.check(0);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setTab(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private void setTab(int idx){
        RadioButton rb=(RadioButton)rgChannel.getChildAt(idx);
        rb.setChecked(true);
        int left=rb.getLeft();
        int width=rb.getMeasuredWidth();
        DisplayMetrics metrics=new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int screenWidth=metrics.widthPixels;
        int len=left+width/2-screenWidth/2;
        hvChannel.smoothScrollTo(len,0);
    }
}
