package com.braintest.brain.test;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SetActivity extends AppCompatActivity {

    private String VersionStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        BrainTestApp.setStatusBar(this,true);
        VersionStr = "Version " + BuildConfig.VERSION_NAME;

        TextView tvVersion = findViewById(R.id.version);
        tvVersion.setText(VersionStr);

        RelativeLayout aboutRl = findViewById(R.id.rl_about);
        RelativeLayout shareRl = findViewById(R.id.rl_share);
        shareRl.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.str_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"));
        });

        aboutRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialogAbout();
            }
        });

        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    private void showDialogAbout() {
        final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // before
        dialog.setContentView(R.layout.layout_about);
        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) dialog.findViewById(R.id.tv_version)).setText(VersionStr);



        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.str_google_play), getPackageName());
            Uri uri = Uri.parse(format);
            Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
            Log.d("---","---format="+format);
            goToMarket.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(goToMarket);

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



}