package com.team.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.team.app.R;
import com.google.android.material.tabs.TabLayout;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class SliderAdapter extends PagerAdapter {

    private ArrayList<Integer> IMAGES;
    private ArrayList<String> URLs;
    private ArrayList<String> Links;
    private final LayoutInflater inflater;
    private final Context context;
    int mode=0;
    TabLayout tabDots;
    ViewPager viewPager;

    public SliderAdapter(Context context, ViewPager viewPager, TabLayout tabDots) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.viewPager=viewPager;
        this.tabDots=tabDots;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        if(mode==0) {
            return IMAGES.size();
        }
        else{
            return URLs.size();
        }
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup view, final int position) {
        View imageLayout = inflater.inflate(R.layout.main_slide, view, false);
        assert imageLayout != null;
        final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
        if(mode==0) {
            imageView.setImageResource(IMAGES.get(position));
        }
        if(mode==1){
            Picasso.get().load(URLs.get(position)).fit().into(imageView);
        }
        imageLayout.setOnClickListener(v -> {
            if(Links.get(position).contains("https://t.me")) {
                context.startActivity(new Intent(Intent.ACTION_VIEW)
                        .setData(Uri.parse(Links.get(position))));
            } else if(Links.get(position).contains("https://api.whatsapp.com/send?")) {
                context.startActivity(new Intent(Intent.ACTION_VIEW)
                        .setData(Uri.parse(Links.get(position))));
            } else {
                context.startActivity(new Intent(Intent.ACTION_VIEW)
                        .setData(Uri.parse(Links.get(position))));
            }
        });
        view.addView(imageLayout, 0);
        return imageLayout;
    }

    @Override
    public boolean isViewFromObject(View view, @NonNull Object object) {
        return view.equals(object);
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    public void setResourceImages(ArrayList<Integer> IMAGES,ArrayList<String> Links){
        this.IMAGES=IMAGES;
        this.Links=Links;
        this.mode=0;
    }

    public void setLinkImages(ArrayList<String> URLs,ArrayList<String> Links){
        this.URLs=URLs;
        this.Links=Links;
        this.mode=1;
    }

}
