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.ApiService;
import com.cygnusa.hyperm.util.DepthPageTransformerNew;
import com.cygnusa.hyperm.util.MainApplication;
import com.cygnusa.hyperm.util.TransparentProgressDialog;
import com.cygnusa.hyperm.wrappers.FragmentItem;

import java.util.ArrayList;
import java.util.Objects;

public class AmTeamMyRankingFragment extends Fragment {

    public static Handler.Callback callback;
    View mainView;
    private ViewPager leaderBoardPager;
    private SmartTabLayout leaderBoardPagerTab;
    TransparentProgressDialog progressDialog;
    ApiService restService;
    private ArrayList<String> MY_LEADER_BOARD = new ArrayList<>();
    ArrayList<FragmentItem> fragments = new ArrayList<>();

    public AmTeamMyRankingFragment() {

    }


    public void onResumeFragment() {
        try {
            if (leaderBoardPager != null) {
                LeaderBoardListFragment fragment = (LeaderBoardListFragment) fragments.get(leaderBoardPager.getCurrentItem())
                        .getFragment();
                fragment.onResumeFragment();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @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;
        restService = ((MainApplication) Objects.requireNonNull(getActivity()).getApplication()).getClient();
        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 LeaderBoardAdapter(getChildFragmentManager(),
                new ArrayList<String>()));
        leaderBoardPager.setAdapter(new LeaderBoardAdapter(getChildFragmentManager(),
                MY_LEADER_BOARD));
        leaderBoardPagerTab.setViewPager(leaderBoardPager);

        leaderBoardPager.post(new Runnable() {
            @Override
            public void run() {
                new Handler(AMLeaderBoardListFragment.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(AMLeaderBoardListFragment.callback).sendEmptyMessage(0);
                } else {
                    new Handler(MyRankingAssessmentListFragment.callback).sendEmptyMessage(0);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public class LeaderBoardAdapter extends FragmentStatePagerAdapter {

        ArrayList<String> categories;


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

        @Override
        public Fragment getItem(int position) {

            if (position == 0) {
                AMLeaderBoardListFragment fragment = new AMLeaderBoardListFragment();
                FragmentItem item = new FragmentItem();
                item.setFragment(fragment);
                item.setTitle(categories.get(position));
                fragments.add(item);
                return fragment;
            } else {
                MyRankingAssessmentListFragment fragment = new MyRankingAssessmentListFragment();
                FragmentItem item = new FragmentItem();
                item.setFragment(fragment);
                item.setTitle(categories.get(position));
                fragments.add(item);
                return fragment;
            }


        }

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


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