package com.cygnusa.hyperm.fragments;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.SEHomeActivity;
import com.cygnusa.hyperm.slider.Animations.DescriptionAnimation;
import com.cygnusa.hyperm.slider.Indicators.PagerIndicator;
import com.cygnusa.hyperm.slider.SliderLayout;
import com.cygnusa.hyperm.slider.SliderTypes.BaseSliderView;
import com.cygnusa.hyperm.slider.SliderTypes.DefaultSliderView;
import com.cygnusa.hyperm.slider.Tricks.ViewPagerEx;
import com.cygnusa.hyperm.util.ApiService;
import com.cygnusa.hyperm.util.GlobalData;
import com.cygnusa.hyperm.util.MainApplication;
import com.cygnusa.hyperm.util.TransparentProgressDialog;



public class SEHomeFragment extends Fragment implements BaseSliderView.OnSliderClickListener,
        ViewPagerEx.OnPageChangeListener {

    public static Handler.Callback callback;
    View mainView;
    ApiService restService;
    TransparentProgressDialog progressDialog;
    private SliderLayout mDemoSlider;
    private LinearLayout menuLayout;
    private boolean mUserSeen = false;
    private boolean mViewCreated = false;

    int yearValue, monthValue;


    public SEHomeFragment() {

    }

    public void onResumeFragment() {

    }

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

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mainView = view;
        restService = ((MainApplication) getActivity().getApplication()).getClient();
        mDemoSlider = mainView.findViewById(R.id.slider);
        menuLayout = mainView.findViewById(R.id.menuLayout);
        callback = new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                setBannerSlider();
                return false;
            }
        };

        String header = "<b>ACHIEVEMENT</b> \n<font color='#ccffffff'>/20 ACHIEVEMENT</font>";
        mainView.findViewById(R.id.myTraining).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Handler(SEHomeActivity.homeFragmentCallback).sendEmptyMessage(0);
            }
        });

        mainView.findViewById(R.id.myTrainingText).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mainView.findViewById(R.id.myTraining).performClick();
            }
        });
        mainView.findViewById(R.id.myAssessment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Handler(SEHomeActivity.homeFragmentCallback).sendEmptyMessage(1);
            }
        });
        mainView.findViewById(R.id.myAssessmentText).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mainView.findViewById(R.id.myAssessment).performClick();
            }
        });
        mainView.findViewById(R.id.myLeaderBoard).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Handler(SEHomeActivity.homeFragmentCallback).sendEmptyMessage(4);
            }
        });
        mainView.findViewById(R.id.myLeaderBoardText).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mainView.findViewById(R.id.myLeaderBoard).performClick();
            }
        });


        mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Default);
        PagerIndicator indicators = mainView.findViewById(R.id.custom_indicator);
        mDemoSlider.setCustomIndicator(indicators);
        mDemoSlider.setCustomAnimation(new DescriptionAnimation());
        mDemoSlider.setDuration(4000);
        mDemoSlider.addOnPageChangeListener(this);
        mViewCreated = true;
        onViewCreatedFirstSight();

    }

    @SuppressLint("CheckResult")
    private void setBannerSlider() {
        mDemoSlider.removeAllSliders();
        RequestOptions requestOptions = new RequestOptions();
        requestOptions
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.placeholder);

        for (int i = 0; i < GlobalData.BANNERS.size(); i++) {
            DefaultSliderView sliderView = new DefaultSliderView(getActivity());
            sliderView
                    .image(GlobalData.BANNERS.get(i).getImage().trim())
                    .setRequestOption(requestOptions)
                    .setBackgroundColor(Color.TRANSPARENT)
                    .setProgressBarVisible(true)
                    .setOnSliderClickListener(this);
            sliderView.bundle(new Bundle());
            mDemoSlider.addSlider(sliderView);
        }


    }


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

    @Override
    public void onSliderClick(BaseSliderView baseSliderView) {

    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {

    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    public View getSlider() {
        return mDemoSlider;
    }

    public View getMenuLayout() {
        return menuLayout;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mViewCreated = false;
        mUserSeen = false;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (!mUserSeen && isVisibleToUser) {
            mUserSeen = true;
            onViewCreatedFirstSight();
        }
    }


    @SuppressLint("StaticFieldLeak")
    private void onViewCreatedFirstSight() {
        if (mUserSeen && mViewCreated) {

        }
    }

}
