package com.application.tak.takapplication;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import android.widget.Toast;

import com.application.tak.takapplication.data_access.Config;
import com.application.tak.takapplication.data_access.GetClassBySignupCode;
import com.application.tak.takapplication.interfaces.OnDBRequestFinished;

import java.util.regex.Pattern;

/**
 * Created by Aleksandra on 04.05.2017.
 */
public class actStudentLoginCode  extends Fragment {

    Context ctx;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.act_student_login_code, container, false);

        ctx = this.getContext();
        AutoCompleteTextView classCodeTv = (AutoCompleteTextView) rootView.findViewById(R.id.code_input);

        if(classCodeTv != null)
        {

classCodeTv.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(!hasFocus)
        {
            if(CheckCodeIsNumber(classCodeTv.getText().toString())) {
                PerformCheck(Integer.parseInt(classCodeTv.getText().toString()));

            }
            else{
                Toast.makeText(ctx,"Nieprawidłowy kod!", Toast.LENGTH_LONG).show();
                if(Config.LoggedInStudent != null)
                {
                    Config.LoggedInStudent.set_School(null);
                    Config.LoggedInStudent.set_Class(null);
                }

            }
        }
    }
});

            classCodeTv.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {


                        if(CheckCodeIsNumber(classCodeTv.getText().toString())) {
                            PerformCheck(Integer.parseInt(classCodeTv.getText().toString()));
                        }
                        else{
                            Toast.makeText(ctx,"Nieprawidłowy kod!", Toast.LENGTH_LONG).show();
                            if(Config.LoggedInStudent != null)
                            {
                                Config.LoggedInStudent.set_School(null);
                                Config.LoggedInStudent.set_Class(null);
                            }
                        }

                    }
                    return false;
                }
            });
        }
        return rootView;
    }

    public void PerformCheck(Integer code)
    {
        if(Config.LoggedInStudent != null)
        {
        GetClassBySignupCode signupChecker = new GetClassBySignupCode(ctx,code);
        signupChecker.setDBRequestFinishedListener(new OnDBRequestFinished() {
    @Override
    public void onDBRequestFinished() {
        if(signupChecker._class != null) {
            Config.LoggedInStudent.set_Class(signupChecker._class);
            Config.LoggedInStudent.set_ClassId(signupChecker._class.get_Id());
            Config.LoggedInStudent.set_School(signupChecker._class.get_School());

            ((actStudentLogin)getActivity()).SetCurrentItem(2);

        }
        else if(signupChecker.dbResult != null){

                Config.LoggedInStudent.set_School(null);
                Config.LoggedInStudent.set_Class(null);

        }
    }

});
    }
    }

    public boolean CheckCodeIsNumber(String code)
    {
        if(Pattern.matches("^\\d{4,}$", code))
        {
            return true;
        }
        return false;
    }


}
