package com.cpm.lorealbaautomation.promooffer;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.cpm.lorealbaautomation.Database.Lorealba_Database;
import com.cpm.lorealbaautomation.R;
import com.cpm.lorealbaautomation.constant.CommonFunctions;
import com.cpm.lorealbaautomation.constant.CommonString;
import com.cpm.lorealbaautomation.delegates.PrefHelper;
import com.cpm.lorealbaautomation.gsonGetterSetter.MasterPromoOffer;


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


public class PromoOffersActivity extends AppCompatActivity {
    String visit_date, username;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor = null;
    RecyclerView recycl_promooffers;
    Lorealba_Database db;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_promo_offers);

        context = this;
        db = new Lorealba_Database(context);
        db.open();
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        if (!CommonFunctions.isTablet(context)) {
            toolbar.setTitleTextAppearance(context, R.style.changestext_sizefor_mobile);
        }

        iduserinterface();
        setTitle("Promo Offers - " + username);
    }

    private void iduserinterface() {
       preferences = PrefHelper.getPrefs(context);
        editor = preferences.edit();
        username = preferences.getString(CommonString.KEY_USERNAME, "");
        visit_date = preferences.getString(CommonString.KEY_DATE, "");
        recycl_promooffers = (RecyclerView) findViewById(R.id.recycl_promooffers);
        db.open();
        final ArrayList<MasterPromoOffer>[] masterPromoOffers = new ArrayList[]{new ArrayList<>()};
        final ArrayList<MasterPromoOffer>[] finalPromoOffers = new ArrayList[]{new ArrayList<>()};

        masterPromoOffers[0] = db.getmaterPromoOffers();
        if (!masterPromoOffers[0].isEmpty()) {
            finalPromoOffers[0] = new ArrayList<>();
            for (int k = 0; k < masterPromoOffers[0].size(); k++) {
                if (CommonFunctions.isTablet(context)) {
                    String path = CommonString.getPromosFolder(context) + masterPromoOffers[0].get(k).getPromoOfferTab();
                    File dir = new File(path);
                    if (dir.exists()) {
                        finalPromoOffers[0].add(masterPromoOffers[0].get(k));
                    }
                } else {
                    String path = CommonString.getPromosFolder(context) + masterPromoOffers[0].get(k).getPromoOfferMobile();
                    File dir = new File(path);
                    if (dir.exists()) {
                        finalPromoOffers[0].add(masterPromoOffers[0].get(k));
                    }
                }
            }
        }


        recycl_promooffers.setAdapter(new ValueAdapter(context, finalPromoOffers[0]));
        recycl_promooffers.setLayoutManager(new LinearLayoutManager(context));
    }

    private class ValueAdapter extends RecyclerView.Adapter<ValueAdapter.MyViewHolder> {
        LayoutInflater inflator;
        List<MasterPromoOffer> data;

        public ValueAdapter(Context context, List<MasterPromoOffer> data) {
            inflator = LayoutInflater.from(context);
            this.data = data;
        }


        @NonNull
        @Override
        public ValueAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
            View view = inflator.inflate(R.layout.linear_layout_promo, parent, false);
            return new ValueAdapter.MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull final ValueAdapter.MyViewHolder holder, final int position) {
            final MasterPromoOffer object = data.get(position);
            if (CommonFunctions.isTablet(context)) {
                String path = CommonString.getPromosFolder(context) + object.getPromoOfferTab();
                File dir = new File(path);
                if (dir.exists()) {
                    Bitmap myBitmap = BitmapFactory.decodeFile(dir.getAbsolutePath());
                    holder.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());
                    holder.imageView.setImageBitmap(myBitmap);
                }
            }

            holder.imageView.setOnClickListener(view -> {
                startActivity(new Intent(context, PromoOfferDetailsActivity.class).putExtra(CommonString.KEY_PROMO_ID, object.getPromoId()));
                overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
            });
        }

        @Override
        public int getItemCount() {
            return data.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            ImageView imageView;

            public MyViewHolder(View itemView) {
                super(itemView);
                imageView = (ImageView) itemView.findViewById(R.id.image);
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // as you specify a parent activity in AndroidManifest.xml.
        if (item.getItemId() == android.R.id.home) {
            overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
            PromoOffersActivity.this.finish();
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
        PromoOffersActivity.this.finish();
    }
}
