package com.example.generator;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.PickVisualMediaRequest;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;


import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Objects;

public class MakeStartingActivity extends AppCompatActivity {

    private ImageView imageView;
    private int numPos = -1;

    private int mainColorResP = 0;
    private int bgTintResP = 0;
    private String ColorP = "";
    private int mainColorResS = 0;
    private int bgTintResS = 0;
    private String ColorS = "";
    private EditText numPosEdit;
    private String Base64img = "";
    private final ActivityResultLauncher<PickVisualMediaRequest> pickMedia =
            registerForActivityResult(new ActivityResultContracts.PickVisualMedia(), uri -> {
                if (uri != null) {
                    imageView.setImageURI(uri);

                    Base64img = convertUriToBase64(uri);

                    if (!Base64img.isEmpty()) {
                        Log.d("Base64Result", Base64img);
                    }
                }
            });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_make);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        switch (EventsActivity.Data.getColorS()) {
            case "Orange":
                ColorS = "Orange";
                mainColorResS = R.color.orangeMain;
                bgTintResS = R.color.orangeMain_bg;
                break;
            case "Green":
                ColorS = "Green";
                mainColorResS = R.color.greenMain;
                bgTintResS = R.color.greenMain_bg;
                break;
            case "Blue":
                ColorS = "Blue";
                mainColorResS = R.color.blueMain;
                bgTintResS = R.color.blueMain_bg;
                break;
            case "Pink":
                ColorS = "Pink";
                mainColorResS = R.color.pinkMain;
                bgTintResS = R.color.pinkMain_bg;
                break;
            case "Purple":
                ColorS = "Purple";
                mainColorResS = R.color.purpleMain;
                bgTintResS = R.color.purpleMain_bg;
                break;
            case "Red":
                ColorS = "Red";
                mainColorResS = R.color.redMain;
                bgTintResS = R.color.redMain_bg;
                break;
            case "Yellow":
                ColorS = "Yellow";
                mainColorResS = R.color.yellowMain;
                bgTintResS = R.color.yellowMain_bg;
                break;
        }

        switch (EventsActivity.Data.getColorP()) {
            case "Orange":
                ColorP = "Orange";
                mainColorResP = R.color.orangeMain;
                bgTintResP = R.color.orangeMain_bg;
                break;
            case "Green":
                ColorP = "Green";
                mainColorResP = R.color.greenMain;
                bgTintResP = R.color.greenMain_bg;
                break;
            case "Blue":
                ColorP = "Blue";
                mainColorResP = R.color.blueMain;
                bgTintResP = R.color.blueMain_bg;
                break;
            case "Pink":
                ColorP = "Pink";
                mainColorResP = R.color.pinkMain;
                bgTintResP = R.color.pinkMain_bg;
                break;
            case "Purple":
                ColorP = "Purple";
                mainColorResP = R.color.purpleMain;
                bgTintResP = R.color.purpleMain_bg;
                break;
            case "Red":
                ColorP = "Red";
                mainColorResP = R.color.redMain;
                bgTintResP = R.color.redMain_bg;
                break;
            case "Yellow":
                ColorP = "Yellow";
                mainColorResP = R.color.yellowMain;
                bgTintResP = R.color.yellowMain_bg;
                break;
        }

        imageView = findViewById(R.id.imageView);
        Button nextButton = (Button) findViewById(R.id.nextButton);
        Button backButton = (Button) findViewById(R.id.backButton);
        Button chooseButton = (Button) findViewById(R.id.button_image);
        numPosEdit = findViewById(R.id.numPos);

        if (mainColorResP != 0) {

            int actualColor = ContextCompat.getColor(this, mainColorResP);

            // 2. Turn that color into a ColorStateList
            ColorStateList tintList = ColorStateList.valueOf(actualColor);


            ViewCompat.setBackgroundTintList(nextButton, tintList);
            ViewCompat.setBackgroundTintList(backButton, tintList);
            ViewCompat.setBackgroundTintList(chooseButton, tintList);
        }



        backButton.setOnClickListener((v) -> {
            Intent intent = new Intent(this, ColorActivity.class);
            startActivity(intent);
        });

        nextButton.setOnClickListener((v) -> {
            if (numPosEdit.getText().toString().trim().isEmpty()) {
                Toast.makeText(this, "choose a number", Toast.LENGTH_SHORT).show();
            } else {
                numPos = Integer.parseInt(numPosEdit.getText().toString().trim());
                if (Objects.equals(Base64img, "")) {
                    Toast.makeText(this, "choose a image", Toast.LENGTH_SHORT).show();
                    Log.d("test", Base64img);
                } else {
                    EventsActivity.Data.setNumPos(numPos);
                    EventsActivity.Data.setBase64(Base64img);
                    Intent intent = new Intent(this, StartingPreview.class);
                    startActivity(intent);
                }
            }
        });

        chooseButton.setOnClickListener((v) -> {
            pickMedia.launch(new PickVisualMediaRequest.Builder()
                    .setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE)
                    .build());
        });


    }
    private String convertUriToBase64(Uri uri) {
        try {
            InputStream inputStream = getContentResolver().openInputStream(uri);

            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            if (inputStream != null) inputStream.close();

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            byte[] imageBytes = outputStream.toByteArray();

            return Base64.encodeToString(imageBytes, Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "Failed to convert image", Toast.LENGTH_SHORT).show();
            return "";
        }


    }

}