package com.cpm.lorealbaautomation.dailyactivity;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import androidx.cardview.widget.CardView;
import androidx.appcompat.widget.Toolbar;

import android.view.MenuItem;
import android.view.View;

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

public class SkinGeniusCategoryActivity extends AppCompatActivity implements View.OnClickListener {
    CardView skingenius_uploadscreenshot_img, skin_genius_img, skin_maybeline_img;
    String CounterId, visit_date, username, skingenius_url = "", maybelline_ur = "";
    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_skin_genius_category);
        context = this;
        db = new Lorealba_Database(context);
        db.open();
        iduserinterface();
        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);
        }

        setTitle("Skin Genius Category - " + username);

    }

    private void iduserinterface() {
        preferences = PrefHelper.getPrefs(context);
        editor = preferences.edit();
        CounterId = preferences.getString(CommonString.KEY_COUNTER_ID, "");
        username = preferences.getString(CommonString.KEY_USERNAME, "");
        visit_date = preferences.getString(CommonString.KEY_DATE, "");
        skingenius_url = preferences.getString(CommonString.KEY_SKIN_GENIUS_URL, "");
        maybelline_ur = preferences.getString(CommonString.KEY_SKIN_MAYBELLINE_URL, "");
        skingenius_uploadscreenshot_img = (CardView) findViewById(R.id.skingenius_uploadscreenshot_img);
        skin_genius_img = (CardView) findViewById(R.id.skin_genius_img);
        skin_maybeline_img = (CardView) findViewById(R.id.skin_maybeline_img);
        skingenius_uploadscreenshot_img.setOnClickListener(this);
        skin_genius_img.setOnClickListener(this);
        skin_maybeline_img.setOnClickListener(this);
    }

    void openUrlInChrome(String url) {
        try {
            try {
                Uri uri = Uri.parse("googlechrome://navigate?url=" + url);
                Intent i = new Intent(Intent.ACTION_VIEW, uri);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
            } catch (ActivityNotFoundException e) {
                Uri uri = Uri.parse(url);
                // Chrome is probably not installed
                // OR not selected as default browser OR if no Browser is selected as default browser
                Intent i = new Intent(Intent.ACTION_VIEW, uri);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
            }
        } catch (Exception ex) {
            ex.fillInStackTrace();
        }
    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.skingenius_uploadscreenshot_img) {
            context.startActivity(new Intent(context, SkinGeniusScreenshotActivity.class));
            overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
        } else if (v.getId() == R.id.skin_genius_img) {
            if (skingenius_url != null && !skingenius_url.equals("")) {
                openUrlInChrome(skingenius_url);
                db.open();
                int openskingenius_count = 1;
                long l = db.insertOpenSkinGeniusdata(username, visit_date, CounterId, CommonFunctions.getUUID(context), openskingenius_count);
            } else {
                AlertandMessages.showToastMsg(context, "Skin Genius URL not found");
            }

        } else if (v.getId() == R.id.skin_maybeline_img) {
            if (maybelline_ur != null && !maybelline_ur.equals("")) {
                openUrlInChrome(maybelline_ur);
                db.open();
                int open_maybelline_count = 1;
                long l = db.insertOpenMaybellineNVdata(username, visit_date, CounterId, CommonFunctions.getUUID(context), open_maybelline_count);
            } else {
                AlertandMessages.showToastMsg(context, "Maybelline URL not found");
            }
        }
    }

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


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == android.R.id.home) {

            overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
            SkinGeniusCategoryActivity.this.finish();
        }
        return super.onOptionsItemSelected(item);
    }


}
