package com.cscodetech.movers.ui;

import static com.cscodetech.movers.utils.SessionManager.intro;
import static com.cscodetech.movers.utils.SessionManager.login;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.cscodetech.movers.R;
import com.cscodetech.movers.databinding.ActivityIntroBinding;
import com.cscodetech.movers.fragment.OneFragment;
import com.cscodetech.movers.fragment.TreeFragment;
import com.cscodetech.movers.fragment.TwoFragment;
import com.cscodetech.movers.model.UserLogin;
import com.google.android.material.tabs.TabLayout;

public class IntroActivity extends BaseActivity {

    private ActivityIntroBinding binding;
    ViewPager vpPager;
    MyPagerAdapter adapterViewPager;
    TextView btnNext;
    int selectPage = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityIntroBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        btnNext = binding.btnNext;
        vpPager = binding.vpPager;

        if(sessionmanager.getBooleanData(login)){
            startActivity(new Intent(IntroActivity.this, HomeActivity.class));
            finish();
        }
        adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
        vpPager.setAdapter(adapterViewPager);
        TabLayout extensiblePageIndicator = binding.flexibleIndicator;
        extensiblePageIndicator.setupWithViewPager(vpPager);

        vpPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {


            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.d("TAG", "onPageScrolled - position: " + position + ", positionOffset: " + positionOffset + ", positionOffsetPixels: " + positionOffsetPixels);
            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onPageSelected(int position) {
                selectPage = position;
                switch (position) {
                    case 0:
                        binding.btnSkip.setVisibility(View.VISIBLE);
                        btnNext.setText(R.string.next);
                        binding.txtTitle.setText(R.string.onet);
                        binding.txtSubtitle.setText(R.string.onest);
                        break;
                    case 1:
                        binding.btnSkip.setVisibility(View.VISIBLE);
                        btnNext.setText(R.string.next);
                        binding.txtTitle.setText(R.string.twot);
                        binding.txtSubtitle.setText(R.string.twost);
                        break;
                    case 2:
                        binding.btnSkip.setVisibility(View.GONE);
                        btnNext.setText(R.string.getstarted);
                        binding.txtTitle.setText(R.string.treet);
                        binding.txtSubtitle.setText(R.string.treest);
                        break;
                    default:
                        break;
                }
            }


            @Override
            public void onPageScrollStateChanged(int state) {
                Log.d("TAG", "onPageScrolled - position: " + state );
            }
        });
        btnNext.setOnClickListener(this::onClick);
        binding.btnSkip.setOnClickListener(this::onClick);
    }

    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_next) {
            if (selectPage == 0) {
                vpPager.setCurrentItem(1);
            } else if (selectPage == 1) {
                vpPager.setCurrentItem(2);
            } else if (selectPage == 2) {
                sessionmanager.setBooleanData(intro, true);
                UserLogin user = new UserLogin();
                user.setId("0");
                user.setName("User");
                user.setEmail("user@gmail.com");
                user.setMobile("+91 8888888888");
                sessionmanager.setUserDetails(user);
                startActivity(new Intent(IntroActivity.this, LoginActivity.class));
                finish();
            }
        } else if (id == R.id.btn_skip) {
            sessionmanager.setBooleanData(intro, true);
            UserLogin user = new UserLogin();
            user.setId("0");
            user.setName("User");
            user.setEmail("user@gmail.com");
            user.setMobile("+91 8888888888");
            sessionmanager.setUserDetails(user);
            startActivity(new Intent(IntroActivity.this, LoginActivity.class));
            finish();
        }
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {
        private int anInt = 3;

        public MyPagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

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

        @Override
        public Fragment getItem(int position) {

            switch (position) {
                case 0:

                    return new OneFragment();
                case 1:

                    return new TwoFragment();
                case 2:

                    return new TreeFragment();
                default:
                    return null;
            }

        }

        @Override
        public CharSequence getPageTitle(int position) {
            Log.e("page", "" + position);
            return "";
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Fragment fragment = (Fragment) super.instantiateItem(container, position);
            return fragment;
        }

    }
}
