package eleriaqueen.seccy;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import com.bumptech.glide.Glide;

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final int blank = R.drawable.blank;
        final int images[] = {R.drawable.viridia, R.drawable.greenill,
                R.drawable.skyly, R.drawable.bluefull,
                R.drawable.purplenum, R.drawable.pinkal,
                R.drawable.redria, R.drawable.oran,
                R.drawable.yellowboze, R.drawable.whitill};

        final ImageView iView[] = new ImageView[13];

        iView[0] = findViewById(R.id.iv0);
        iView[1] = findViewById(R.id.iv1);
        iView[2] = findViewById(R.id.iv2);
        iView[3] = findViewById(R.id.iv3);
        iView[4] = findViewById(R.id.iv4);
        iView[5] = findViewById(R.id.iv5);
        iView[6] = findViewById(R.id.iv6);
        iView[7] = findViewById(R.id.iv7);
        iView[8] = findViewById(R.id.iv8);
        iView[9] = findViewById(R.id.iv9);
        iView[10] = findViewById(R.id.iv10);
        iView[11] = findViewById(R.id.iv11);
        iView[12] = findViewById(R.id.iv12);

        // Preload images with Glide
        myGlide.preloadAll(images, MainActivity.this);
        /* Start by loading placeholders in all views */
        myGlide.loadImages(blank, iView, MainActivity.this);

        final ImageView ivLegacy = iView[0];
        final TableLayout table = findViewById(R.id.table_layout);
        final TableRow huRow = findViewById(R.id.hu_img);
        final TableRow raRow = findViewById(R.id.ra_img);
        final TableRow foRow = findViewById(R.id.fo_img);
        final EditText nameEditText = findViewById(R.id.editText);

        final SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("myPref", MODE_PRIVATE);

        //Restore Name from previous session and process it
        String restoredEditTxt = sharedPref.getString("savedText", "");
        if (restoredEditTxt != null) {
            nameEditText.setText(restoredEditTxt);

            /* There are 12 + 1 image views  */
            int imgTotal = 12;

            String input_str = restoredEditTxt;
            int nameLen = input_str.length();

            psoName psoname = new psoName(input_str);

            if ((nameLen >= 1) && (nameLen <= 10)) {
                /* Load images in their respective "BB" views and set up Section ID names */
                for (int i = 0; i <= (imgTotal - 1); i++) {
                    int imgCurOne = psoname.bbVal[i];
                    myGlide.loadOneImage(images[imgCurOne], iView[i + 1], MainActivity.this);
                }
            }

            if ((nameLen > 10) && (nameLen <= 12)) {
                for (int i = 1; i <= imgTotal; i++) {
                    myGlide.loadImages(blank, iView, MainActivity.this);
                }
            }

            /* Load image in "Legacy" ImageView */
            if ((nameLen >= 1) && (nameLen <= 12) && checkIsAscii(input_str)) {
                int imgCurTwo = psoname.legacyVal;
                myGlide.loadOneImage(images[imgCurTwo], iView[0],MainActivity.this);
            } else {
                /* Load a blank image in "Legacy" ImageView */
                myGlide.loadOneImage(blank, iView[0], MainActivity.this);
            }
        }

        // Restore preference values
        int restorediv0Val = sharedPref.getInt("iv0IsVisible", 1);
        if (restorediv0Val != 1) {
            ivLegacy.setVisibility(View.GONE);
        }

        int restoredTblVal = sharedPref.getInt("tblIsVisible", 1);
        if (restoredTblVal != 1) {
            table.setVisibility(View.GONE);
        }

        int restoredHuRowVal = sharedPref.getInt("huRowIsVisible", 1);
        if (restoredHuRowVal != 1) {
            huRow.setVisibility(View.GONE);
        }

        int restoredRaRowVal = sharedPref.getInt("raRowIsVisible", 1);
        if (restoredRaRowVal != 1) {
            raRow.setVisibility(View.GONE);
        }

        int restoredFoRowVal = sharedPref.getInt("foRowIsVisible", 1);
        if (restoredFoRowVal != 1) {
            foRow.setVisibility(View.GONE);
        }
        // Restore end


        final TextView legacy_text = findViewById(R.id.tvClassic);
        legacy_text.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if (ivLegacy.getVisibility() == View.VISIBLE){
                    myPref.writeInt(sharedPref, "iv0IsVisible", 0);
                    ivLegacy.setVisibility(View.GONE);
                }
                else if (ivLegacy.getVisibility() != View.VISIBLE){
                    myPref.writeInt(sharedPref, "iv0IsVisible", 1);
                    ivLegacy.setVisibility(View.VISIBLE);
                }
            }
        });
        ivLegacy.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if (ivLegacy.getVisibility() == View.VISIBLE){
                    myPref.writeInt(sharedPref, "iv0IsVisible", 0);
                    ivLegacy.setVisibility(View.GONE);
                }
                else if (ivLegacy.getVisibility() != View.VISIBLE){
                    myPref.writeInt(sharedPref, "iv0IsVisible", 1);
                    ivLegacy.setVisibility(View.VISIBLE);
                }
            }
        });

        final TextView bb_text = findViewById(R.id.tvBB);
        bb_text.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if (table.getVisibility() == View.VISIBLE){
                    myPref.writeInt(sharedPref, "tblIsVisible", 0);
                    table.setVisibility(View.GONE);
                }
                else if (table.getVisibility() != View.VISIBLE){
                    myPref.writeInt(sharedPref, "tblIsVisible", 1);
                    table.setVisibility(View.VISIBLE);
                }
            }
        });

        final TableRow hu_text = findViewById(R.id.hu_txt);
        hu_text.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if (huRow.getVisibility() == View.VISIBLE){
                    myPref.writeInt(sharedPref, "huRowIsVisible", 0);
                    huRow.setVisibility(View.GONE);
                }
                else if (huRow.getVisibility() != View.VISIBLE){
                    myPref.writeInt(sharedPref, "huRowIsVisible", 1);
                    huRow.setVisibility(View.VISIBLE);
                }
            }
        });
        huRow.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if (huRow.getVisibility() == View.VISIBLE){
                    myPref.writeInt(sharedPref, "huRowIsVisible", 0);
                    huRow.setVisibility(View.GONE);
                }
                else if (huRow.getVisibility() != View.VISIBLE){
                    myPref.writeInt(sharedPref, "huRowIsVisible", 1);
                    huRow.setVisibility(View.VISIBLE);
                }
            }
        });

        final TableRow ra_text = findViewById(R.id.ra_txt);
        ra_text.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if (raRow.getVisibility() == View.VISIBLE){
                    myPref.writeInt(sharedPref, "raRowIsVisible", 0);
                    raRow.setVisibility(View.GONE);
                }
                else if (raRow.getVisibility() != View.VISIBLE){
                    myPref.writeInt(sharedPref, "raRowIsVisible", 1);
                    raRow.setVisibility(View.VISIBLE);
                }
            }
        });
        raRow.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if (raRow.getVisibility() == View.VISIBLE){
                    myPref.writeInt(sharedPref, "raRowIsVisible", 0);
                    raRow.setVisibility(View.GONE);
                }
                else if (raRow.getVisibility() != View.VISIBLE){
                    myPref.writeInt(sharedPref, "raRowIsVisible", 1);
                    raRow.setVisibility(View.VISIBLE);
                }
            }
        });

        final TableRow fo_text = findViewById(R.id.fo_txt);
        fo_text.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if (foRow.getVisibility() == View.VISIBLE){
                    myPref.writeInt(sharedPref, "foRowIsVisible", 0);
                    foRow.setVisibility(View.GONE);
                }
                else if (foRow.getVisibility() != View.VISIBLE){
                    myPref.writeInt(sharedPref, "foRowIsVisible", 1);
                    foRow.setVisibility(View.VISIBLE);
                }
            }
        });
        foRow.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if (foRow.getVisibility() == View.VISIBLE){
                    myPref.writeInt(sharedPref, "foRowIsVisible", 0);
                    foRow.setVisibility(View.GONE);
                }
                else if (foRow.getVisibility() != View.VISIBLE){
                    myPref.writeInt(sharedPref, "foRowIsVisible", 1);
                    foRow.setVisibility(View.VISIBLE);
                }
            }
        });

        nameEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable sequence) {
            }

            @Override
            public void beforeTextChanged(CharSequence sequence, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence sequence, int start, int before, int count) {
                /* There are 12 + 1 image views  */
                int imgTotal = 12;

                String input_str = sequence.toString();
                int nameLen = input_str.length();

                psoName psoname = new psoName(input_str);

                // Start by clearing all imgviews
                if (input_str.equals("")) {
                    myGlide.loadImages(blank, iView, MainActivity.this);
                }

                if ((nameLen >= 1) && (nameLen <= 10)) {
                    /* Load images in their respective "BB" views and set up Section ID names */
                    for (int i = 0; i <= (imgTotal - 1); i++) {
                        int imgCurOne = psoname.bbVal[i];
                        myGlide.loadOneImage(images[imgCurOne], iView[i + 1], MainActivity.this);
                    }
                }

                if ((nameLen > 10) && (nameLen <= 12)) {
                    myGlide.loadImages(blank, iView, MainActivity.this);
                }

                /* Load image in "Legacy" ImageView */
                if ((nameLen >= 1) && (nameLen <= 12) && checkIsAscii(input_str)) {
                    int imgCurTwo = psoname.legacyVal;
                    myGlide.loadOneImage(images[imgCurTwo], iView[0],MainActivity.this);
                } else {
                    /* Load a blank image in "Legacy" ImageView */
                    myGlide.loadOneImage(blank, iView[0], MainActivity.this);
                }

                // Save Name in preferences
                myPref.writeStr(sharedPref, "savedText", input_str);
            }
        });
    }


    private boolean checkIsAscii(String s)
    {
        int cval;
        for (int i = 0; i < s.length(); i++)
        {
            cval = s.charAt(i);
            if ((cval < 0x20) || (cval > 0x7F)) return false;
        }
        return true;
    }
}

class myPref {
    public static void writeInt(SharedPreferences pref, String id, int val) {
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt(id, val);
        editor.apply();
    }

    public static void writeStr(SharedPreferences pref, String id, String val) {
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(id, val);
        editor.apply();
    }
}

class myGlide {
    public static void preloadAll(int[] image, Activity activity)
    {
        for (int img : image)
        {
            Glide.with(activity).load(img).preload();
        }
    }

    public static void loadImages(int image, ImageView[] iv, Activity activity)
    {
        for (ImageView imgview : iv)
        {
            Glide.with(activity).load(image).into(imgview);
        }
    }

    public static void loadOneImage(int image, ImageView iv, Activity activity)
    {
        Glide.with(activity).load(image).into(iv);

    }
}


