package com.app.rewardapp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.app.rewardapp.Config.Config;
import com.app.rewardapp.R;
import com.app.rewardapp.Responsemodel.IntroModel;
import com.app.rewardapp.adapters.IntroAdapter;
import com.app.rewardapp.util.Session;
import com.google.android.material.tabs.TabLayout;
import com.ornach.nobobutton.NoboButton;

import java.util.ArrayList;
import java.util.List;

public class IntroActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private RelativeLayout nextLayout, getStartedLayout;
    private IntroAdapter adapter;
    private List<IntroModel> introModels;
    private TabLayout tabLayout;
    private Button getStarted;
    private ImageView goNext;
    Session session;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intro);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        session = new Session(this);
        session.setBoolean(session.FIRSTTIME,false);
        viewPager = findViewById(R.id.vp4);
        nextLayout = findViewById(R.id.ll7);
        getStartedLayout = findViewById(R.id.ll8);
        introModels = new ArrayList<>();
        introModels.add(new IntroModel(Config.SLIDE_ONE_ICON, Config.SLIDE_ONE_HEADLINE,  Config.SLIDE_ONE_DESCRIPTION));
        introModels.add(new IntroModel(Config.SLIDE_TWO_ICON, Config.SLIDE_TWO_HEADLINE,  Config.SLIDE_TWO_DESCRIPTION));
        introModels.add(new IntroModel(Config.SLIDE_THREE_ICON, Config.SLIDE_THREE_HEADLINE,  Config.SLIDE_THREE_DESCRIPTION));
        adapter = new IntroAdapter(getApplicationContext(), introModels);
        viewPager.setAdapter(adapter);
        viewPager.setPageTransformer(false, new DepthPagerTransformer());

        tabLayout = findViewById(R.id.tabLayout1);
        tabLayout.setupWithViewPager(viewPager);

        goNext = findViewById(R.id.goNext);
        getStarted = findViewById(R.id.getStarted);
        goNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = viewPager.getCurrentItem();
                if (position < introModels.size()) {
                    position++;
                    nextLayout.setVisibility(View.VISIBLE);
                    getStartedLayout.setVisibility(View.GONE);
                    viewPager.setCurrentItem(position);
                } else if (position == introModels.size() - 1) {
                    doVisibilityOperation();
                }
            }
        });

        getStarted.setOnClickListener(v -> startActivity(new Intent(IntroActivity.this,FrontLogin.class)));

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getPosition() == introModels.size() - 1) {
                    doVisibilityOperation();
                } else {
                    nextLayout.setVisibility(View.VISIBLE);
                    getStartedLayout.setVisibility(View.GONE);
                    getStarted.animate().setInterpolator(new BounceInterpolator()).scaleXBy(1).scaleX(0).scaleYBy(1).scaleY(0);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void doVisibilityOperation() {
        getStartedLayout.setVisibility(View.VISIBLE);
        nextLayout.setVisibility(View.GONE);
        getStarted.animate()
                .setInterpolator(new BounceInterpolator())
                .setDuration(1000)
                .scaleXBy(0)
                .scaleX(1)
                .scaleYBy(0)
                .scaleY(1);
    }

    private class DepthPagerTransformer implements ViewPager.PageTransformer {
        public static final float MIN_SCALE = 0.75f;

        @Override
        public void transformPage(@NonNull View page, float position) {
            int pageWidth = page.getWidth();

            if (position < -1) {
                //left most
                page.setAlpha(0);
            } else if (position <= 0) {
                //from center to first left
                page.setAlpha(1);
                page.setTranslationX(0);
                page.setScaleY(1);
                page.setScaleX(1);

            } else if (position <= 1) {
                //at center to first right
                page.setAlpha(1 - position);
                page.setTranslationX(pageWidth * -position);

                float scaling = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
                page.setScaleX(scaling);
                page.setScaleY(scaling);
                page.animate().setInterpolator(new AccelerateDecelerateInterpolator());
            } else {
                page.setAlpha(0);
            }

        }
    }
}
