package com.bernard.mcqinator.api.frageable;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bernard.mcqinator.R;
import com.bernard.mcqinator.api.fragen.Frage;
import com.bernard.mcqinator.api.fragen.TextFrage;
import com.bernard.mcqinator.api.fragenStats.QuizStat;
import com.bernard.mcqinator.view.activities.MainActivity;
import com.bernard.mcqinator.view.activities.fragenActivities.FrageActivity;
import com.bernard.mcqinator.view.activities.fragenActivities.TextFrageActivity;

/**
 * @author mysaa
 * @date on 3/13/18.
 */

public interface TextFrageable<S extends QuizStat,ID extends Frage.Identifier> extends Frageable<TextFrageable.TextData,TextFrage.Reply,TextFrageable.TextConfigurator,S,ID,TextFrage>{

    String LAST_FRAGE_COUNT = "com.bernard.qcminator.LAST_TEXT_FRAGE_COUNT";
    int DEFAULT_FRAGE_COUNT = 10;
    int DEFAULT_FRAGE_COUNT_MAX = 50;


    class TextData extends Frageable.Data{
        public final int length;


        public TextData(int length) {
            this.length = length;
        }

        public int getLength() {
            return length;
        }

        @Override
        public Class<? extends FrageActivity<? extends Frage>> getFrageActivityClass() {
            return TextFrageActivity.class;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(length);
        }

        public static Parcelable.Creator<TextFrageable.TextData> CREATOR = new Parcelable.Creator<TextFrageable.TextData>(){
            @Override
            public TextFrageable.TextData createFromParcel(Parcel source) {
                return new TextFrageable.TextData(source.readInt());
            }

            @Override
            public TextFrageable.TextData[] newArray(int size) {
                return new TextFrageable.TextData[size];
            }
        };
    }

    class TextConfigurator extends Frageable.Configurator{

        @Override
        public View attachConfigLayout(final Activity a, ViewGroup frame) {
            final View layout = a.getLayoutInflater().inflate(R.layout.text_frage_options_layout,frame);
            final TextView frageCountText = layout.findViewById(R.id.frageCountText);
            final SeekBar frageCountBar = layout.findViewById(R.id.frageCount);

            frageCountBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    frageCountText.setText(a.getResources().getString(R.string.fragCount,progress));
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            });

            View.OnLongClickListener frageCountLongListener = new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    Log.v("frageCount","looooooooooooong");
                    AlertDialog.Builder builder = new AlertDialog.Builder(a);
                    builder.setTitle(R.string.frageCountAsk);
                    final EditText frageCountPrompt = new EditText(a);
                    frageCountPrompt.setInputType(InputType.TYPE_CLASS_NUMBER);
                    builder.setView(frageCountPrompt);
                    builder.setPositiveButton(android.R.string.ok,new DialogInterface.OnClickListener(){
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            try {
                                int n = Integer.parseInt(frageCountPrompt.getText().toString(), 10);
                                if(n>DEFAULT_FRAGE_COUNT_MAX)
                                    frageCountBar.setMax(n);
                                else
                                    frageCountBar.setMax(DEFAULT_FRAGE_COUNT_MAX);
                                frageCountBar.setProgress(n);
                            }catch(Exception e){
                                Log.e("FrageCountDialog","Cannot read data",e);
                                Toast.makeText(a, R.string.failedSettingFrageCountValue, Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                    builder.setNegativeButton(android.R.string.no,new DialogInterface.OnClickListener(){
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();


                    return false;
                }
            };
            frageCountText.setLongClickable(true);
            frageCountBar.setLongClickable(true);

            frageCountBar.setOnLongClickListener(frageCountLongListener);
            frageCountText.setOnLongClickListener(frageCountLongListener);

            SharedPreferences preferences = a.getSharedPreferences(MainActivity.PREFERENCES_FILE_KEY,Context.MODE_PRIVATE);

            frageCountBar.setProgress(preferences.getInt(LAST_FRAGE_COUNT,DEFAULT_FRAGE_COUNT));

            return layout;
        }

        @Override
        public int getTypStringID() {
            return R.string.blocQuiz;
        }

        @Override
        public int getIconDrawableID() {
            return R.drawable.bloc_quiz_icon;
        }

        @Override
        public Data genDataFromConfigLayout(View v) {
            SeekBar fC = v.findViewById(R.id.frageCount);
            return new TextData(fC.getProgress());
        }
    }

}