package com.example.leudaemialikeme.Fragment;

import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.PopupWindow;
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.Activity.GoAnswerActivity;
import com.example.leudaemialikeme.Activity.SendInvitationActivity;
import com.example.leudaemialikeme.Activity.SendQuestionActivity;
import com.example.leudaemialikeme.Adapter.InvitationPageFragmentAdapter;
import com.example.leudaemialikeme.R;

public class CommunityFragment 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 InvitationPageFragmentAdapter adapter; //viewPager 的适配器
    private PopupWindow popupWindow;
    private ImageButton tab_add;
    private Button bar_send_invitation;
    private Button bar_send_question;
    private Button bar_answer;
    //列表数据

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

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

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

            rgChannel.setOnCheckedChangeListener(
                    new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                            viewPager.setCurrentItem(checkedId);
                        }
                    }
            );
            tab_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //获取自定义菜单的布局文件
                    View popupWindow_view=getLayoutInflater().inflate(R.layout.community_menu,null,false);
                    //创建popupWindow实例，设置菜单的宽度和高度
                    popupWindow = new PopupWindow(popupWindow_view, ActionBar.LayoutParams.WRAP_CONTENT,ActionBar.LayoutParams.WRAP_CONTENT,true);
                    //设置菜单显示在按钮的下面
                    popupWindow.showAsDropDown(view.findViewById(R.id.tab_add),0,0);
                    //单机其它位置隐藏菜单
                    popupWindow_view.setOnTouchListener(new View.OnTouchListener(){
                        @Override
                        public boolean onTouch(View v, MotionEvent event){
                            //如果菜单存在并且处于显示状态
                            if (popupWindow!=null&&popupWindow.isShowing()){
                                popupWindow.dismiss();//关闭菜单
                                popupWindow=null;
                            }
                            return false;
                        }
                    });
                    bar_send_invitation = (Button)popupWindow_view.findViewById(R.id.bar_send_invitation);
                    bar_send_question = (Button)popupWindow_view.findViewById(R.id.bar_send_question);
                    bar_answer = (Button)popupWindow_view.findViewById(R.id.bar_answer);
                    bar_send_invitation.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(getActivity(), SendInvitationActivity.class);
                            startActivity(intent);
                        }
                    });
                    bar_send_question.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(getActivity(), SendQuestionActivity.class);
                            startActivity(intent);
                        }
                    });
                    bar_answer.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(getActivity(), GoAnswerActivity.class);
                            startActivity(intent);
                        }
                    });
                }
            });
        }
        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() {
        FragmentManager fragmentManager = super.getActivity().getSupportFragmentManager();
        adapter=new InvitationPageFragmentAdapter(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;
        hvChannel.smoothScrollTo(len,0);
    }


}