package com.example.cmknowledgegraph;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.ChatContentPkg.RecyclerViewFragment;
import com.github.florent37.materialviewpager.MaterialViewPager;
import com.github.florent37.materialviewpager.header.HeaderDesign;

public class ChatContent extends Fragment{
    AppCompatActivity appCompatActivity;//获取到Activity

    public AppCompatActivity getAppCompatActivity() {
        return appCompatActivity;
    }

    public void setAppCompatActivity(AppCompatActivity appCompatActivity) {
        this.appCompatActivity = appCompatActivity;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);


    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //要加载的layout文件
        return inflater.inflate(R.layout.activity_chat, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //包装处理逻辑
        //得到MaterViewPager实例
        MaterialViewPager materialViewPager = (MaterialViewPager) getActivity().findViewById(R.id.materialViewPager);
        //为MaterialViewPager添加监听
        materialViewPager.setMaterialViewPagerListener(new MaterialViewPager.Listener() {
            @Override
            //设置转换动画和图片，颜色
            public HeaderDesign getHeaderDesign(int page) {
                switch (page){
                    case 0:
                        return HeaderDesign.fromColorResAndDrawable(
                                R.color.blue,
                                getResources().getDrawable(R.drawable.draw5)
                        );
                    case 1:
                        return HeaderDesign.fromColorResAndDrawable(
                                R.color.green,
                                getResources().getDrawable(R.drawable.draw6)
                        );
                    case 2:
                        return HeaderDesign.fromColorResAndDrawable(
                                R.color.cyan,
                                getResources().getDrawable(R.drawable.draw7)
                        );
                }

                return null;
            }
        });
        //设置Toolbar
        Toolbar toolbar = materialViewPager.getToolbar();
        AppCompatActivity ap = getAppCompatActivity();
        if(toolbar!=null){
            ap.setSupportActionBar(toolbar);
            ActionBar actionBar = ap.getSupportActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowHomeEnabled(true);
            actionBar.setDisplayShowTitleEnabled(true);
            actionBar.setDisplayUseLogoEnabled(false);
            actionBar.setHomeButtonEnabled(true);
        }
        //为Viewpager设置适配器
        ViewPager viewPager = materialViewPager.getViewPager();
        viewPager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                switch (position % 3){
                    case 0:
                        return RecyclerViewFragment.newInstance();
                    default:
                        return RecyclerViewFragment.newInstance();

                }
            }

            @Override
            public int getCount() {
                return 3;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                switch (position % 3){
                    case 0:
                        return "论坛";
                    case 1:
                        return "drug";
                    case 2:
                        return "药友";
                    default:
                        return "TAPN";
                }
            }
        });
        materialViewPager.getViewPager().setOffscreenPageLimit(materialViewPager
                .getViewPager().getAdapter().getCount());
        materialViewPager.getPagerTitleStrip().setViewPager(materialViewPager.getViewPager());

    }

    @Override
    public void onResume() {
        super.onResume();
    }
}
