package com.team.app;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.team.app.adapter.SliderAdapter;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.TimerTask;

public class SliderView extends LinearLayout {

    private ArrayList<Integer> IMAGES;
    private ArrayList<String> URLs;
    private ArrayList<String> Links;
    TimerTask updatePage;
    SliderAdapter sliderAdapter;
    int currentPage=0;
    int mode;
    ViewPager viewPager;
    TabLayout tabDots;
    public SliderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        inflate(getContext(),R.layout.slide_view,this);
        viewPager=findViewById(R.id.vp);
        tabDots=findViewById(R.id.tabDots);
        sliderAdapter = new SliderAdapter(context,viewPager,tabDots);
        tabDots.setupWithViewPager(viewPager,true);
        //sliderAdapter.execute();
    }


    public void setResourceImages(ArrayList<Integer> IMAGES,ArrayList<String> Links){
        this.IMAGES=IMAGES; this.Links = Links;
        sliderAdapter.setResourceImages(IMAGES,Links);
        viewPager.setAdapter(sliderAdapter);
    }

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

    public TimerTask getTimerTask(){
        final Handler handler = new Handler();
        final Runnable Update = new Runnable() {
            public void run() {
                if (currentPage == 3) {
                    currentPage = 0;
                }
                viewPager.setCurrentItem(currentPage++, true);
            }
        };
        updatePage=new TimerTask() {
            @Override
            public void run() {
                handler.post(Update);
            }
        };
        return updatePage;
    }
}
