package com.friendvilla.bucksbuddy;

import static android.widget.Toast.LENGTH_SHORT;
import static android.widget.Toast.makeText;
import static com.friendvilla.bucksbuddy.Method.title;
import static com.friendvilla.bucksbuddy.Method.updateUser;
import static com.friendvilla.bucksbuddy.controls.Constant.LINK;
import static com.friendvilla.bucksbuddy.controls.Constant.Tag;
import static com.friendvilla.bucksbuddy.controls.Constant.VERSION;
import static java.util.Locale.getDefault;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Base64;
import android.widget.TextView;

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.lifecycle.MutableLiveData;

import com.friendvilla.bucksbuddy.controls.UpdateBox;
import com.friendvilla.bucksbuddy.model.User;
import com.google.android.material.navigation.NavigationView;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    public static MutableLiveData<User> USER = new MutableLiveData<>();

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigation = findViewById(R.id.nav_view);
        ((TextView)findViewById(R.id.tvTitle)).setText(title(this,getString(R.string.app_name),5));
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawer,R.string.app_name,R.string.app_name);
        drawer.addDrawerListener(toggle); toggle.syncState();
        findViewById(R.id.ivMenu).setOnClickListener(view -> {
            if(drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                drawer.openDrawer(GravityCompat.START);
            }
        });
        if(VERSION != BuildConfig.VERSION_CODE) {
            UpdateBox updateBox = new UpdateBox();
            updateBox.show(getSupportFragmentManager(),Tag);
        }
        if(!new String(Base64.decode(BuildConfig.PACKAGE,Base64.DEFAULT)).equals(BuildConfig.APPLICATION_ID)) {
            finish();
        }
        USER.observe(this, user -> {
            BigDecimal bal = new BigDecimal(user.getDeposit()).add(new BigDecimal(user.getWithdraw()));
            ((TextView)findViewById(R.id.tvBal)).setText("₹ " + bal);
        });
        findViewById(R.id.bal).setOnClickListener(view -> startActivity(new Intent(this,WalletActivity.class)));
        findViewById(R.id.check).setOnClickListener(view -> {
            assert USER.getValue() != null; Date c = Calendar.getInstance().getTime();
            String check = new SimpleDateFormat("dd-MMM-yyyy",getDefault()).format(c);
            BigDecimal p = new BigDecimal(USER.getValue().getDeposit()).add(new BigDecimal("1"));
            String dpst = p.toString(); String wtdrl = USER.getValue().getWithdraw();
            Dialog d = new Dialog(this); d.setContentView(R.layout.checkin);
            d.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            d.findViewById(R.id.btn).setOnClickListener(v -> {
                USER.setValue(new User(dpst,wtdrl,USER.getValue().getRefercode(),check,USER.getValue().getReferby()));
                updateUser(this,dpst,wtdrl,"0");
                d.dismiss();
                makeText(this,"Check in Successfully Done!",LENGTH_SHORT).show();
            });
            assert USER.getValue() != null;
            if(USER.getValue().getCheckin().equals(check)) {
                makeText(this,"Today you already Checked!",LENGTH_SHORT).show();
            } else {
                d.show();
            }
        });
        findViewById(R.id.spin).setOnClickListener(view -> startActivity(new Intent(this,SpinActivity.class)));
        findViewById(R.id.slot).setOnClickListener(view -> startActivity(new Intent(this,SlotActivity.class)));
        findViewById(R.id.scratch).setOnClickListener(view -> startActivity(new Intent(this,ScratchActivity.class)));
        findViewById(R.id.guess).setOnClickListener(view -> startActivity(new Intent(this,NumberActivity.class)));
        findViewById(R.id.tictac).setOnClickListener(view -> startActivity(new Intent(this,TictacActivity.class)));
        findViewById(R.id.refer).setOnClickListener(view -> startActivity(new Intent(this,RuleActivity.class).putExtra("act",1)));
        findViewById(R.id.rule).setOnClickListener(view -> startActivity(new Intent(this,RuleActivity.class).putExtra("act",2)));
        navigation.setNavigationItemSelectedListener(item -> {
            int id = item.getItemId();
            drawer.closeDrawer(GravityCompat.START);
            if (id == R.id.rate) {
                Uri ruri = Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName());
                Intent rate = new Intent(Intent.ACTION_VIEW,ruri);
                startActivity(rate);
            } else if (id == R.id.contact) {
                new AlertDialog.Builder(this)
                        .setMessage(getString(R.string.cnt))
                        .setPositiveButton("OK",(d,i) -> d.dismiss()).create().show();
            } else if (id == R.id.policy) {
                Uri puri = Uri.parse("https://friendvilla.xyz/privacy.html");
                Intent policy = new Intent(Intent.ACTION_VIEW).setData(puri);
                startActivity(policy);
            } else if (id == R.id.refund) {
                Uri ruri = Uri.parse("https://friendvilla.xyz/refund.html");
                Intent refund = new Intent(Intent.ACTION_VIEW).setData(ruri);
                startActivity(refund);
            } else if (id == R.id.disclaimer) {
                Uri duri = Uri.parse("https://friendvilla.xyz/disclaimer.html");
                Intent disclaimer = new Intent(Intent.ACTION_VIEW).setData(duri);
                startActivity(disclaimer);
            } else if (id == R.id.share) {
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("text/plain");
                i.putExtra(Intent.EXTRA_SUBJECT,"Share App");
                i.putExtra(Intent.EXTRA_TEXT,getString(R.string.sharetxt) + LINK);
                startActivity(Intent.createChooser(i, "Share By"));
            } else if (id == R.id.exit) {
                new AlertDialog.Builder(this)
                        .setMessage("Are you sure you want to exit?")
                        .setCancelable(false)
                        .setPositiveButton("Yes",(dialog,i) -> {
                            dialog.dismiss();
                            finish();
                        })
                        .setNegativeButton("No",(d,i) -> d.dismiss())
                        .show();
            }
            return true;
        });
    }

    @Override
    public void onBackPressed() {
        Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.exit_dialog);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.findViewById(R.id.ads).setOnClickListener(view -> {
            startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("https://play.google.com/store/apps/details?id=com.friendvilla.quotes")));
            dialog.dismiss();finish();
        });
        dialog.findViewById(R.id.txt_rate).setOnClickListener(view -> {
            startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("https://play.google.com/store/apps/dev?id=7861050630849506029")));
            dialog.dismiss();finish();
        });
        dialog.findViewById(R.id.txt_later).setOnClickListener(view -> {
            dialog.dismiss();
            finish();
        });
        dialog.show();
    }
}