package com.iq.intelligence.study;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.balysv.materialripple.BuildConfig;

public class MainActivity extends AppCompatActivity {

    TextView startBtn, shareBtn, exitBtn, aboutBtn;

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

        startBtn = findViewById(R.id.btn_start);
        shareBtn = findViewById(R.id.btn_share_game);
        aboutBtn = findViewById(R.id.btn_about_game);
        exitBtn = findViewById(R.id.btn_exit);
        startBtn.setOnClickListener(v -> {
            Intent page2 = new Intent(MainActivity.this, LevelsPageActivity.class);
            startActivity(page2);
        });


        shareBtn.setOnClickListener(v -> {
            Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("text/plain");
            share.putExtra(Intent.EXTRA_SUBJECT, R.string.app_name);
            String text = getString(R.string.share_title);
            text = text + "\n https://play.google.com/store/apps/details?id=" + getPackageName();
            share.putExtra(Intent.EXTRA_TEXT, text);
            startActivity(Intent.createChooser(share, "choose one"));
        });

        aboutBtn.setOnClickListener(v -> showDialogAbout());

        exitBtn.setOnClickListener(v -> finish());
    }


    private void showDialogAbout() {
        final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // before
        dialog.setContentView(R.layout.game_introduce);
        dialog.setCancelable(true);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        TextView tvVersion = (TextView) dialog.findViewById(R.id.tv_version);

        try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            tvVersion.setText(String.format(getString(R.string.app_v), packageInfo.versionName));
        } catch (PackageManager.NameNotFoundException exception) {
            tvVersion.setText(String.format(getString(R.string.app_v), "1.0.1"));
        }


        dialog.findViewById(R.id.bt_privacy_policy).setOnClickListener(v -> {
            Uri parse = Uri.parse(PrescribedValue.URL_PRIVACY);

            Intent moreApp = new Intent(Intent.ACTION_VIEW);
            moreApp.setData(parse);
            startActivity(moreApp);

        });

        dialog.findViewById(R.id.bt_close).setOnClickListener(v -> dialog.dismiss());


        dialog.findViewById(R.id.bt_rateUs).setOnClickListener(v -> {
            String format = String.format(getString(R.string.google_play), getPackageName());
            Uri uri = Uri.parse(format);
            Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
            Log.d("---", "---format=" + format);
            startActivity(goToMarket);

        });

        dialog.show();
        dialog.getWindow().setAttributes(lp);
    }


}
