package com.cpm.lorealbaautomation.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.cpm.lorealbaautomation.R;
import com.cpm.lorealbaautomation.constant.CommonString;
import com.cpm.lorealbaautomation.gsonGetterSetter.MasterPromoOffer;
import com.cpm.lorealbaautomation.promooffer.PromoOfferDetailsActivity;

import java.io.File;
import java.util.ArrayList;

public class MyPager extends PagerAdapter {
    Context context;
    ArrayList<MasterPromoOffer> list;

    public MyPager(Context context, ArrayList<MasterPromoOffer> list) {
        this.context = context;
        this.list = list;
    }

    /*
    This callback is responsible for creating a page. We inflate the layout and set the drawable
    to the ImageView based on the position. In the end we add the inflated layout to the parent
    container .This method returns an object key to identify the page view, but in this example page view
    itself acts as the object key
    */
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.linear_layout, null);
        ImageView imageView = view.findViewById(R.id.image);
        final MasterPromoOffer object = list.get(position);

        if (object.getPromoId()!=null && !object.getPromoId().isEmpty() && object.getPromoId().equalsIgnoreCase("No Data")){
            imageView.setImageResource(R.mipmap.no_promo_banner);
        }else {
            if (isTablet(context)) {
                String path = CommonString.getPromosFolder(context) + object.getPromoOfferTab();
                File dir = new File(path);
                if (dir.exists()) {
                    Bitmap myBitmap = BitmapFactory.decodeFile(dir.getAbsolutePath());
                    imageView.setImageBitmap(myBitmap);
                }
            } else {
                String path = CommonString.getPromosFolder(context) + object.getPromoOfferMobile();
                File dir = new File(path);
                if (dir.exists()) {
                    Bitmap myBitmap = BitmapFactory.decodeFile(dir.getAbsolutePath());
                    imageView.setImageBitmap(myBitmap);
                }
            }


            imageView.setOnClickListener(view1 -> context.startActivity(new Intent(context, PromoOfferDetailsActivity.class)
                    .putExtra(CommonString.KEY_PROMO_ID,object.getPromoId())));
        }

        container.addView(view);
        return view;
    }

    /*
    This callback is responsible for destroying a page. Since we are using view only as the
    object key we just directly remove the view from parent container
    */
    @Override
    public void destroyItem(ViewGroup container, int position, @NonNull Object view) {
        container.removeView((View) view);
    }

    /*
    Returns the count of the total pages
    */
    @Override
    public int getCount() {
        return list.size();
    }

    /*
    Used to determine whether the page view is associated with object key returned by instantiateItem.
    Since here view only is the key we return view==object
    */
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return object == view;
    }

    public boolean isTablet(Context context) {
        boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
        boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
        return (xlarge || large);
    }
}