package com.cpm.lorealbaMabeline.promooffer;

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

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

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

import java.io.File;

public class PromoOfferDetailsActivity extends AppCompatActivity {
    MasterPromoOffer object = new MasterPromoOffer();
    TextView promostart_txt, promoend_txt, promo_description;
    ImageView image;
    String visit_date, username, promoId = "";
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor = null;
    Lorealba_Database db;
    Context context;

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

        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);
        }


        if (getIntent().getStringExtra(CommonString.KEY_PROMO_ID) != null) {
            promoId = getIntent().getStringExtra(CommonString.KEY_PROMO_ID);
        }


        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, "");
        promostart_txt = (TextView) findViewById(R.id.promostart_txt);
        promoend_txt = (TextView) findViewById(R.id.promoend_txt);
        promo_description = (TextView) findViewById(R.id.promo_description);
        image = (ImageView) findViewById(R.id.image);
        db.open();

        object = db.getmaterPromoOffersbypromoId(promoId);

        if (object != null && object.getPromoId() != null) {
            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());
                    image.setImageBitmap(myBitmap);
                }
            } else {
                String path = CommonString.getPromosFolder(context) + object.getPromoOfferMobile();
                File dir = new File(path);
                if (dir.exists()) {
                    Bitmap myBitmap = BitmapFactory.decodeFile(dir.getAbsolutePath());
                    image.setImageBitmap(myBitmap);
                }
            }
        }

        promostart_txt.setText(object.getStartDate());
        promoend_txt.setText(object.getEndDate());
        promo_description.setText(object.getDescription());

    }

    @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);
            PromoOfferDetailsActivity.this.finish();
        }

        return super.onOptionsItemSelected(item);
    }

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