package com.example.leudaemialikeme.Fragment;

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

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

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


public class MessageFragment extends Fragment 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 String[] channelList = {"        私信      ","       评论       ",
            "       赞      ","       粉丝       "}; //默认的内导航栏目
    private MessagePageFragmentAdapter adapter; //viewPager 的适配器

    public MessageFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(view==null){
            view=inflater.inflate(R.layout.fragment_message, container, false);
            viewPager=(ViewPager)view.findViewById(R.id.vpNewsList0);
            initViewPager(); //设置 ViewPager

            rgChannel=(RadioGroup)view.findViewById(R.id.rgChannel0);
            hvChannel=(HorizontalScrollView)view.findViewById(R.id.hvChannel0);
            initTab(inflater);

            rgChannel.setOnCheckedChangeListener(
                    new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                            viewPager.setCurrentItem(checkedId);
                        }
                    }
            );
        }
        return view;
    }

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

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

    @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();
        super.getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int screenWidth=metrics.widthPixels;
       // int len=left+width/2-screenWidth/2;
        int len=left+width/2-screenWidth/2;
        hvChannel.smoothScrollTo(len,0);
    }
    public MessagePageFragmentAdapter getAdapter(){
        return adapter;
    }
}