package com.application.tak.takapplication;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
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_model.User;
import com.application.tak.takapplication.services.LoginStatus;

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

    private FloatingActionButton fabyes;
    private AutoCompleteTextView autoName;
    private AutoCompleteTextView autoLastname;
    public AutoCompleteTextView autoClassName;
    public AutoCompleteTextView autoSchoolName;
    private Context ctx;

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

        ctx = this.getContext();
        autoName = (AutoCompleteTextView) rootView.findViewById(R.id.tvloginNameStudent);
        autoLastname = (AutoCompleteTextView) rootView.findViewById(R.id.tvloginNazwiskoStudent);
        autoClassName = (AutoCompleteTextView) rootView.findViewById(R.id.tvloginClassNameStudent);
        autoSchoolName = (AutoCompleteTextView) rootView.findViewById(R.id.tvloginSchoolNameStudent);

        if(autoName != null && Config.LoggedInStudent != null)
            autoName.setText(Config.LoggedInStudent.get_FName());

        if(autoLastname != null && Config.LoggedInStudent != null)
            autoLastname.setText(Config.LoggedInStudent.get_LName());

        String className = "";
        if(Config.LoggedInStudent != null && Config.LoggedInStudent.get_Class() != null)
            className = Config.LoggedInStudent.get_Class().get_ClassName();
        if(autoClassName != null)
            autoClassName.setText(className);

        String schoolName = "";
        if(Config.LoggedInStudent != null && Config.LoggedInStudent.get_School() != null)
            schoolName = Config.LoggedInStudent.get_School().get_SchoolName();
        if(autoSchoolName != null)
            autoSchoolName.setText(schoolName);


        autoClassName.setInputType(InputType.TYPE_NULL);
        autoClassName.setKeyListener(null);
        autoSchoolName.setInputType(InputType.TYPE_NULL);
        autoSchoolName.setKeyListener(null);

        fabyes = (FloatingActionButton) rootView.findViewById(R.id.fabGoToLoginStep2);
        fabyes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(Config.LoggedInStudent == null)
                {
                    ((actStudentLogin)getActivity()).SetCurrentItem(0);
                    return;
                }

                if(!((actStudentLogin)getActivity()).regulationAccepted)
                {
                    Toast.makeText(ctx,"Aby się zarejestrować należy zaakceptować regulamin!", Toast.LENGTH_LONG).show();
                    ((actStudentLogin)getActivity()).SetCurrentItem(2);
                            return;
                }

                if(Config.LoggedInStudent != null)
                {
                    Config.LoggedInStudent.set_FName(autoName.getText().toString());
                    Config.LoggedInStudent.set_LName(autoLastname.getText().toString());
                }

                if(Config.LoggedInStudent != null && Config.LoggedInStudent.get_Class() != null && !Config.LoggedInStudent.get_Username().isEmpty()
                        && !Config.LoggedInStudent.get_ExternAppId().isEmpty() && !Config.LoggedInStudent.get_FName().isEmpty()
                        && !Config.LoggedInStudent.get_LName().isEmpty() && !Config.LoggedInStudent.get_PhoneNo().isEmpty())
                {
                    CreateStudent();
                }
                else
                {
                    Toast.makeText(ctx,"Nieprawidłowe dane.",Toast.LENGTH_LONG).show();
                }

            }
        });

        autoName.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_NEXT))
                {
                    autoLastname.findFocus();
                }
                return false;
            }
        });

        autoLastname.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_NEXT))
                {
                    HideKeyboard();
                }
                return false;
            }
        });




        return rootView;
    }

    void HideKeyboard()
    {
        View focused = getView();
        if (focused != null) {
            focused.clearFocus();
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(focused.getWindowToken(),0);
        }
    }

    void CreateStudent()
    {

        User student = new User();
        student.set_RoleId(1);
        student.set_ClassId(Config.LoggedInStudent.get_ClassId());
        student.set_Username(Config.LoggedInStudent.get_Username());
        student.set_IsLoggedIn(1);
        student.set_PhoneNo(Config.LoggedInStudent.get_PhoneNo());
        student.set_LName(Config.LoggedInStudent.get_LName());
        student.set_FName(Config.LoggedInStudent.get_FName());
        student.set_ExternAppId(Config.LoggedInStudent.get_ExternAppId());
        student.set_IsActive(1);

        LoginStatus logStatus = new LoginStatus(ctx);
        logStatus.NewStudent(student,this.getActivity());

    }
}
