package com.cygnusa.hyperm.fragments;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.util.DepthPageTransformerNew;

import java.util.ArrayList;

public class TeamRankingFragment extends Fragment {

    public static Handler.Callback callback;
    View mainView;
    private ViewPager leaderBoardPager;
    private SmartTabLayout leaderBoardPagerTab;
    private ArrayList<String> MY_LEADER_BOARD = new ArrayList<>();

    public TeamRankingFragment() {

    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_my_leaderboard, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mainView = view;
        leaderBoardPager = mainView.findViewById(R.id.myTrainingPager);
        leaderBoardPagerTab = mainView.findViewById(R.id.myTrainingPagerTab);
        MY_LEADER_BOARD.clear();
        MY_LEADER_BOARD.add("Sales");
        MY_LEADER_BOARD.add("Assessment");
        callback = new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                setLeaderBoardCategory();
                return false;
            }
        };
    }

    private void setLeaderBoardCategory() {
        leaderBoardPager.setPageTransformer(true, new DepthPageTransformerNew());
        leaderBoardPager.setAdapter(new TeamRankingAdapter(getChildFragmentManager(),
                new ArrayList<String>()));
        leaderBoardPager.setAdapter(new TeamRankingAdapter(getChildFragmentManager(),
                MY_LEADER_BOARD));
        leaderBoardPagerTab.setViewPager(leaderBoardPager);

        leaderBoardPager.post(new Runnable() {
            @Override
            public void run() {
                new Handler(TeamRankingSalesFragment.callback).sendEmptyMessage(0);
            }
        });

        leaderBoardPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    new Handler(TeamRankingSalesFragment.callback).sendEmptyMessage(0);
                } else {
                    new Handler(TeamRankingAssessmentFragment.callback).sendEmptyMessage(0);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public class TeamRankingAdapter extends FragmentStatePagerAdapter {

        ArrayList<String> categories;


        private TeamRankingAdapter(FragmentManager fm, ArrayList<String> list) {
            super(fm);
            categories = list;
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return new TeamRankingSalesFragment();
            } else {
                return new TeamRankingAssessmentFragment();
            }
        }

        @Override
        public int getCount() {
            return categories.size();
        }


        @Override
        public CharSequence getPageTitle(int position) {
            return categories.get(position);
        }
    }

}
