package com.application.tak.takapplication;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.application.tak.takapplication.data_access.Config;
import com.application.tak.takapplication.data_model.Adress;
import com.application.tak.takapplication.data_model.User;
import com.application.tak.takapplication.services.LoginStatus;

/**
 * Created by Aleksandra on 15.05.2017.
 */
public class actClientLoginRegulationFragment extends android.support.v4.app.Fragment {
    FloatingActionButton fab;
    Context ctx;
    Activity act;
    public static boolean regulationAccepted = false;

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

        ctx = this.getContext();
        act = getActivity();
        fab = (FloatingActionButton) rootView.findViewById(R.id.fab);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                regulationAccepted = true;
                if(Config.LoggedInClient != null && Config.LoggedInClient.get_Adress() != null
                        && !Config.LoggedInClient.get_Username().isEmpty()&&
                        !Config.LoggedInClient.get_Password().isEmpty() &&
                        !Config.LoggedInClient.get_LName().isEmpty() &&
                        !Config.LoggedInClient.get_FName().isEmpty() &&
                        !Config.LoggedInClient.get_Adress().get_City().isEmpty() &&
                        !Config.LoggedInClient.get_Adress().get_PostCode().isEmpty() &&
                        !Config.LoggedInClient.get_PhoneNo().isEmpty() &&
                        !Config.LoggedInClient.get_Adress().get_Road().isEmpty())
                {
                    LoginStatus lgStatus = new LoginStatus(ctx);
                    User u = Config.LoggedInClient;
                    u.set_IsActive(1);
                    u.set_RoleId(2);

                    Adress a = Config.LoggedInClient.get_Adress();

                    lgStatus.NewClient(u,a,act);

                    ((actClientLogin)getActivity()).SetCurrentItem(2);


                }
                else{
                    ((actClientLogin)getActivity()).SetCurrentItem(0);
                    Toast.makeText(ctx,"Niepoprawne dane",Toast.LENGTH_LONG).show();
                }

            }
        });
        return rootView;
    }
}
