package com.example.geocachingapp;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GameClass extends MainActivity {

    //boolean cardIsDetected;
    //boolean tagIsConnected;

    View gameView;

    static Spinner spinner;

    private Button mChooseGame;
    private Button mNoChooseGame;

    public void showGameWindow(final View view) {

        //Create a View object yourself through inflater
        LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(view.getContext().LAYOUT_INFLATER_SERVICE);
        gameView = inflater.inflate(R.layout.game_select, null);

        //Specify the length and width through constants
        int width = LinearLayout.LayoutParams.WRAP_CONTENT;
        int height = LinearLayout.LayoutParams.WRAP_CONTENT;

        //Make Inactive Items Outside Of PopupWindow
        boolean focusable = true;

        //Create a window with our parameters
        final PopupWindow popupGame = new PopupWindow(gameView, width, height, focusable);

        //Set the location of the window on the screen
        popupGame.showAtLocation(view, Gravity.CENTER, 0, 0);

        //Initialize the elements of our window, install the handler

        spinner = (Spinner) gameView.findViewById(R.id.spinner);


        mChooseGame = (Button) gameView.findViewById(R.id.chooseGame);
        mChooseGame.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                showReward(view, popupGame);
            }
        });

        mNoChooseGame = (Button) gameView.findViewById(R.id.noChooseGame);
        mNoChooseGame.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                popupGame.dismiss();
            }
        });


        gameView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                popupGame.dismiss();
                return true;
            }
        });
    }

    private boolean showReward(View view, PopupWindow popupGame)
    {
        String stringGame = null;
        String gameCode = null;

        stringGame = String.valueOf(spinner.getSelectedItem());

        if (stringGame.equals("Skladačka")){
            try {
                showImage("Skladačka", view);
                popupGame.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        else if (stringGame.equals("Prírodné javy")){
            try {
                showImage("Prírodné javy", view);
                popupGame.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        else
            showSomethingWentWrongError();

        return true;
    }

    public void showSomethingWentWrongError()
    {
        Context context = gameView.getContext();
        CharSequence text = "Niečo sa pokazilo";
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }

}
