package com.nicholasyang.meleestats;

import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.res.ResourcesCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link AddGameFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link AddGameFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AddGameFragment extends Fragment {
    private OnFragmentInteractionListener mListener;

    @Bind(R.id.yourCharacterSpinner) Spinner yourCharacter;
    @Bind(R.id.oppoCharacterSpinner) Spinner oppoCharacter;
    @Bind(R.id.stageSpinner) Spinner stage;
    @Bind(R.id.yourStocksSpinner) Spinner yourStocks;
    @Bind(R.id.oppoStocksSpinner) Spinner oppoStocks;
    @Bind(R.id.serieslogo) ImageView seriesLogo;
    @Bind(R.id.nemesisInput) AutoCompleteTextView nemesisInput;
    @Bind(R.id.button_add_game) Button add;

    public AddGameFragment() {
    }

    public static AddGameFragment newInstance() {
        AddGameFragment fragment = new AddGameFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_add_game, container, false);
        ButterKnife.bind(this, v);
        Integer[] stocks = new Integer[]{0, 1, 2, 3, 4};
        yourCharacter.setAdapter(new ArrayAdapter<Character>(getContext(), android.R.layout.simple_spinner_item, Character.values()));
        oppoCharacter.setAdapter(new ArrayAdapter<Character>(getContext(), android.R.layout.simple_spinner_item, Character.values()));
        stage.setAdapter(new ArrayAdapter<Stage>(getContext(), android.R.layout.simple_spinner_item, Stage.values()));
        yourStocks.setAdapter(new ArrayAdapter<Integer>(getContext(), android.R.layout.simple_spinner_item, stocks));
        oppoStocks.setAdapter(new ArrayAdapter<Integer>(getContext(), android.R.layout.simple_spinner_item, stocks));
        seriesLogo.setImageDrawable(ContextCompat.getDrawable(getContext(), Character.randomCharacter()));
        try {
            nemesisInput.setAdapter(new ArrayAdapter<String>(getContext(), android.R.layout.simple_dropdown_item_1line, NemesisDataAnalysis.getNemeses(getContext())));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return v;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }

    @Override
    public String toString(){
        return "Add game";
    }

    @OnClick(R.id.button_add_game) void add(){
        if ((int) yourStocks.getSelectedItem() != 0 && (int) oppoStocks.getSelectedItem() != 0) {
            add.setText("Someone must have 0 stocks");
            resetButton();
            return;
        }
        if ((int) yourStocks.getSelectedItem() == 0 && (int) oppoStocks.getSelectedItem() == 0) {
            add.setText("You cannot have 0 stocks for both!");
            resetButton();
            return;
        }
        Game game = new Game((Character) yourCharacter.getSelectedItem(), (Character) oppoCharacter.getSelectedItem(), (Stage) stage.getSelectedItem(), (int) yourStocks.getSelectedItem(), (int) oppoStocks.getSelectedItem(), nemesisInput.getText().toString().trim());
        try {
            JSONManager.add(game, getContext());
            ((MainActivity) getActivity()).doCharacterSubMenu();
            ((MainActivity) getActivity()).doNemesisSubMenu();
            add.setText("Game added!");
            add.setClickable(false);
            add.getBackground().setColorFilter(ContextCompat.getColor(getContext(), R.color.materialGreen600), PorterDuff.Mode.SRC_ATOP);
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    if(getActivity() == null){
                        return;
                    }
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            add.setText("Add game");
                            add.getBackground().clearColorFilter();
                            add.setClickable(true);
                        }
                    });
                }
            }, 1500);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void resetButton(){
        add.getBackground().setColorFilter(ContextCompat.getColor(getContext(), R.color.materialRed400), PorterDuff.Mode.SRC_ATOP);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        add.setText("Add game");
                        add.getBackground().clearColorFilter();
                    }
                });
            }
        }, 1500);
    }
}
