package com.cygnusa.hyperm.fragments;


import android.graphics.Color;
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.fancyshowcase.DismissListener;
import com.cygnusa.hyperm.fancyshowcase.FancyShowCaseView;
import com.cygnusa.hyperm.fancyshowcase.FocusShape;
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;

public class LeaderBoardFragment extends Fragment implements DismissListener {

    View mainView;

    ViewPager leaderBoardPager;
    SmartTabLayout leaderBoardPagerTab;
    TransparentProgressDialog progressDialog;
    ApiService restService;
    ArrayList<String> MY_LEADER_BOARD = new ArrayList<>();
    ArrayList<FragmentItem> fragments = new ArrayList<>();
    private int SHOWCASE_POSITION = 0;

    public static Handler.Callback callback;

    public LeaderBoardFragment() {

    }



    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) 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");
       // setLeaderBoardCategory();
        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);

    }



    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){
                LeaderboardSalesSectionFragment fragment = new LeaderboardSalesSectionFragment();
                FragmentItem item = new FragmentItem();
                item.setFragment(fragment);
                item.setTitle(categories.get(position));
                fragments.add(item);
                return fragment;

            }else {
                LeaderBoardAssessmentList fragment = new LeaderBoardAssessmentList();
                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);
        }
    }

    public void showHelpScreen() {
        try {
            SHOWCASE_POSITION = 0;
            new FancyShowCaseView.Builder(getActivity())

                    .title("Tap here to select the category")
                    .titleSize(22, 1)
                    .closeOnTouch(true)
                    .focusShape(FocusShape.ROUNDED_RECTANGLE)
                    .dismissListener(this)
                    .backgroundColor(Color.parseColor("#99000000"))
                    .build()
                    .show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDismiss(String id) {
        try {
            if (SHOWCASE_POSITION == 0) {
                new FancyShowCaseView.Builder(getActivity())
                        .focusOn(leaderBoardPagerTab)
                        .title("Tap or swipe to navigate to next page")
                        .titleSize(22, 1)
                        .closeOnTouch(true)
                        .focusShape(FocusShape.ROUNDED_RECTANGLE)
                        .dismissListener(this)
                        .backgroundColor(Color.parseColor("#99000000"))
                        .build()
                        .show();
            }
            SHOWCASE_POSITION++;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSkipped(String id) {

    }
}
