package com.cygnusa.hyperm;

import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.cygnusa.hyperm.fragments.IndroFragment;
import com.cygnusa.hyperm.util.BackgroundToForegroundTransformerNew;
import com.cygnusa.hyperm.util.ViewPagerCustomDuration;


public class IndroScreen extends AppCompatActivity {

    ViewPagerCustomDuration viewPager;
    SmartTabLayout viewpagerTab;
    FrameLayout frameLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_indro_screen);
        ImageView splashLogo = findViewById(R.id.splashLogo);
        setupWindowAnimations();
        viewPager = findViewById(R.id.viewPager);
        frameLayout = findViewById(R.id.frame);
        viewpagerTab = findViewById(R.id.viewpagerTab);
        viewPager.setPageTransformer(true, new BackgroundToForegroundTransformerNew());
        viewPager.setScrollDurationFactor(3.0);


        frameLayout.setVisibility(View.GONE);


        findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goLogin();

            }
        });

        viewPager.post(new Runnable() {
            @Override
            public void run() {
                viewPager.setAdapter(new IndroAdapter(getSupportFragmentManager()));
                viewpagerTab.setViewPager(viewPager);
            }
        });

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

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 2) {
                    if (frameLayout.getVisibility() == View.GONE)
                        slideToTop(frameLayout);
                } else {
                    if (frameLayout.getVisibility() == View.VISIBLE)
                        slideToBottom(frameLayout);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void slideToBottom(View view) {
        TranslateAnimation animate = new TranslateAnimation(0, 0, 0, view.getHeight());
        animate.setDuration(500);
        animate.setFillAfter(true);
        view.startAnimation(animate);
        view.setVisibility(View.GONE);
    }

    public void slideToTop(View view) {
        TranslateAnimation animate = new TranslateAnimation(0, 0, view.getHeight(), 0);
        animate.setDuration(500);
        animate.setFillAfter(true);
        view.startAnimation(animate);
        view.setVisibility(View.VISIBLE);
    }

    private void setupWindowAnimations() {
        this.overridePendingTransition(R.anim.animation_enter,
                R.anim.animation_leave);
    }

    private void goLogin() {
        startActivity(new Intent(getBaseContext(), LoginActivity.class));
        finish();
    }

    public class IndroAdapter extends FragmentStatePagerAdapter {


        private IndroAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            IndroFragment fragment = new IndroFragment();
            fragment.setPosition(position);
            return fragment;
        }

        @Override
        public int getCount() {
            return 3;
        }


        @Override
        public CharSequence getPageTitle(int position) {
            return "";
        }
    }
}
