package layout;


import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.miss.temp.R;

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

import data.DataAccess;
import models.Category;
import models.Word;

/**
 * A simple {@link Fragment} subclass.
 */
public class AddNewWord extends Fragment implements View.OnClickListener {

    private String[] categoriesNames;
    private List<Category> categories;
    private List<String> wordsList;
    private ArrayAdapter<String> spinnerAdapter;
    public int pos;
    Spinner categoryName;
    Button addNewWordBtn;
    EditText addNewWordEditText;
    Word wordModelToAdd;

    public AddNewWord() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_add_new_word, container, false);

        // this is only for reference how to get the id of a drawable resource by name;
//        Button objectBtn = (Button) view.findViewById(R.id.object_btn);
//        int backgroundReId = Utils.getResId("animal_category");
//        objectBtn.setBackgroundResource(backgroundReId);
        wordsList = new ArrayList<String>();

        Bundle args = this.getArguments();
        categories = new ArrayList<Category>((List<Category>) args.getSerializable("categories_array"));

        new GetWordsTask().execute((DataAccess) args.getSerializable("data"));

        categoryName = (Spinner) view.findViewById(R.id.category_spinner);
        categoryName.setSelection(pos);

        categoriesNames = new String[categories.size() + 1];

        categoriesNames[0] = "Choose category";

        for (int i = 0; i < categories.size(); i++) {
            categoriesNames[i + 1] = categories.get(i).getName();
        }

        spinnerAdapter = new ArrayAdapter<String>(getContext(), R.layout.spinner_item, categoriesNames);
        //categoryName.setOnItemSelectedListener(new CustomOnItemSelectedListener());
        categoryName.setAdapter(spinnerAdapter);

        addNewWordBtn = (Button) view.findViewById(R.id.add_new_word_btn);
        addNewWordBtn.setOnClickListener(this);

        addNewWordEditText = (EditText) view.findViewById(R.id.word_to_add);

        return view;
    }

    @Override
    public void onClick(View v) {
        String wordToAdd = addNewWordEditText.getText().toString()
                                                       .trim();
        String chosenCategoryName = String.valueOf(categoryName.getSelectedItem());
        if (wordToAdd.length() == 0){
            Toast toast = Toast.makeText(getContext(), "Word to add cannot be empty.", Toast.LENGTH_SHORT);
            LinearLayout toastLayout = (LinearLayout) toast.getView();
            TextView toastTV = (TextView) toastLayout.getChildAt(0);
            toastTV.setTextSize(30);
            toastTV.setTextColor(Color.RED);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();

            return;
        } else if(wordsList.contains(wordToAdd.toLowerCase())){
            Toast toast = Toast.makeText(getContext(), "Word already exists.", Toast.LENGTH_SHORT);
            LinearLayout toastLayout = (LinearLayout) toast.getView();
            TextView toastTV = (TextView) toastLayout.getChildAt(0);
            toastTV.setTextSize(30);
            toastTV.setTextColor(Color.RED);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();

            return;
        } else if(chosenCategoryName == "Choose category"){
            Toast toast = Toast.makeText(getContext(), "You must chose category.", Toast.LENGTH_SHORT);
            LinearLayout toastLayout = (LinearLayout) toast.getView();
            TextView toastTV = (TextView) toastLayout.getChildAt(0);
            toastTV.setTextSize(20);
            toastTV.setTextColor(Color.RED);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();

            return;
        }

        int indexOfChosenCategory = 0;
        for (int i = 0; i < categoriesNames.length; i++) {
            if (categoriesNames[i] == chosenCategoryName){
                indexOfChosenCategory = i;
                break;
            }
        }
        Category chosenCategory = categories.get(indexOfChosenCategory);
        wordsList.add(wordToAdd.toLowerCase());
        wordModelToAdd = new Word();
        wordModelToAdd.setContent(wordToAdd);
        wordModelToAdd.setCategoryId(chosenCategory.getId());

        new AddWordInDatabaseTask().execute((DataAccess) getArguments().getSerializable("data"));
    }

    private class GetWordsTask extends AsyncTask<DataAccess, Void, List<String>> {
        @Override
        protected List<String> doInBackground(DataAccess... params) {
            List<String> words = params[0].getAllWordsAsContent();

            return words;
        }

        @Override
        protected void onPostExecute(List<String> words) {
            for (String word : words) {
                wordsList.add(word.toLowerCase());
            }
        }
    }

    private class AddWordInDatabaseTask extends AsyncTask<DataAccess, Void, Long> {
        @Override
        protected Long doInBackground(DataAccess... params) {

            Long word_id = params[0].createWord(wordModelToAdd);

            return word_id;
        }

        @Override
        protected void onPostExecute(Long word_id) {
            Toast toast = Toast.makeText(getContext(), "You successfully added the word \"" + wordModelToAdd.getContent() + "\"", Toast.LENGTH_SHORT);
            LinearLayout toastLayout = (LinearLayout) toast.getView();
            TextView toastTV = (TextView) toastLayout.getChildAt(0);
            toastTV.setTextSize(30);
            toastTV.setTextColor(Color.GREEN);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    }
//
//    public class CustomOnItemSelectedListener implements AdapterView.OnItemSelectedListener {
//
//        @Override
//        public void onItemSelected (AdapterView<?> main, View view, int position,
//                                    long Id) {
//
//            if(position > 0){
////                Toast.makeText(getContext(), position,
////                        Toast.LENGTH_SHORT).show();
//            }else{
////                Toast.makeText(getContext(), "Select category",
////                        Toast.LENGTH_SHORT).show();
//            }
//
//        }
//
//        @Override
//        public void onNothingSelected(AdapterView<?> parent) {
////            Toast.makeText(getContext(), "Select category (on nothing selected)",
////                    Toast.LENGTH_SHORT).show();
//        }
//
//    }
}

@@ -3383,33 +3383,33 @@
 tTV.setTextSize(
-3
+2
 0);%0A            
@@ -3427,35 +3427,37 @@
 TextColor(Color.
-RED
+WHITE
 );%0A            t
@@ -3870,33 +3870,33 @@
 tTV.setTextSize(
-3
+2
 0);%0A            
@@ -3914,35 +3914,37 @@
 TextColor(Color.
-RED
+WHITE
 );%0A            t
@@ -4157,16 +4157,17 @@
 must cho
+o
 se categ
@@ -4169,17 +4169,16 @@
 category
-.
 %22, Toast
@@ -4413,11 +4413,13 @@
 lor.
-RED
+WHITE
 );%0A 
@@ -6289,9 +6289,9 @@
 ize(
-3
+2
 0);%0A
@@ -6333,13 +6333,13 @@
 lor.
-GREEN
+WHITE
 );%0A 

toast message is white