package com.airson.alive.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.airson.alive.R;
import com.airson.alive.activity.base.BaseActivity;
import com.airson.alive.model.UserResponseModel;
import com.airson.alive.network.ApiClient;
import com.airson.alive.network.ApiInterface;
import com.airson.alive.preference.Preferences;
import com.airson.alive.utils.CommonUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Shrikant on 4/22/18.
 */

public class LoginActivity extends BaseActivity{
    @BindView(R.id.et_username)
    EditText userName;
    @BindView(R.id.et_password)
    EditText paswword;
    @BindView(R.id.forgot_password)
    TextView forgotPassword;
    @BindView(R.id.signin)
    Button signin;
    @BindView(R.id.signup)
    TextView signup;
    String usernameString = "", passwordString = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setUnBinder(ButterKnife.bind(this));
    }

    @Override
    protected void setUp() {

    }

    @OnClick(R.id.signin)
    public void OnClickSignIn(View view){

        if (CommonUtils.isInternetOn(this, true)){
            if (isValidInput()){
                showLoading();
                try {
                    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
                    Call<UserResponseModel> call = apiService.getLogin(usernameString, passwordString);
                    call.enqueue(new Callback<UserResponseModel>() {
                        @Override
                        public void onResponse(Call<UserResponseModel> call, Response<UserResponseModel> response) {
                            hideLoading();
                            if(response.body()!=null){
                                if(response.body().isError()==false){

                                    Preferences.getInstance().setAuthToken(LoginActivity.this, response.body().getUser().getAuthtoken());
                                    Preferences.getInstance().setUserName(LoginActivity.this, response.body().getUser().getName());
                                    Preferences.getInstance().setEmail(LoginActivity.this, response.body().getUser().getEmail());
                                    Preferences.getInstance().setMobile(LoginActivity.this, response.body().getUser().getUsername());
                                    Preferences.getInstance().setGender(LoginActivity.this, response.body().getUser().getGender());
                                    Preferences.getInstance().setIsLogin(LoginActivity.this, true);
                                    Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                    startActivity(intent);
                                    finish();
                                    showMessage(response.body().getMessage());
                                } else {
                                    showMessage(response.body().getMessage());
                                }
                            }
//                            else
//                                showMessage("its null");

                        }

                        @Override
                        public void onFailure(Call<UserResponseModel>call, Throwable t) {
                            // Log error here since request failed
                            hideLoading();
                            showMessage(t.getMessage());
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @OnClick(R.id.forgot_password)
    public void OnClickForgotPassword(View view){
        showForgotPasswordDialog();
    }

    @OnClick(R.id.signup)
    public void OnClickSignUp(View view){

        Intent intent = new Intent(this, SignupActivity.class);
        startActivity(intent);
    }

    private boolean isValidInput() {

        Boolean isValid = true;
        usernameString = userName.getText().toString();
        passwordString = paswword.getText().toString();

        if (usernameString ==null || usernameString.isEmpty()) {
            isValid = false;
            onError(getString(R.string.enter_username));

        } else if (usernameString.length()>0 && usernameString.length()<10) {
            isValid = false;
            onError(getString(R.string.invalid_mobile));

        }else  if (passwordString ==null || passwordString.isEmpty()) {
            isValid = false;
            onError(getString(R.string.enter_password));
        }

        return isValid;
    }


    public void showForgotPasswordDialog() {
        String mobileNumber = "";
        final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
        dialog.setContentView(R.layout.dailog_forgotpassword_layout);
        dialog.setCancelable(true);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        Window window = dialog.getWindow();
        lp.copyFrom(window.getAttributes());
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

        EditText mobileET = (EditText) dialog.findViewById(R.id.et_mobile);
        Button resetButton = (Button) dialog.findViewById(R.id.resetbtn);
        TextView signinText = (TextView)dialog.findViewById(R.id.signin);

        signinText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        resetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

//        private boolean isValidMobile() {
//
//            Boolean isValid = true;
//            mobileNumber = mobileET.getText().toString();
//
//            if (mobileNumber ==null || mobileNumber.isEmpty()) {
//                isValid = false;
//                onError(getString(R.string.enter_username));
//
//            } else if (mobileNumber.length()>0 && mobileNumber.length()<10) {
//                isValid = false;
//                onError(getString(R.string.invalid_mobile));
//
//            }
//
//            return isValid;
//        }



//        resendEmail.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (CommonUtils.isInternetOn(MainActivity.this, true)) {
//                    showLoading();
//                    try {
//                        ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
//                        Call<ResponseModel> call = apiService.resendEmail(new ResendEmailRequestModel(Preferences.getInstance().getSocialEmail(MainActivity.this)));
//                        call.enqueue(new Callback<ResponseModel>() {
//                            @Override
//                            public void onResponse(Call<ResponseModel>call, Response<ResponseModel> response) {
//                                hideLoading();
//                                if(response.body()!=null){
//                                    if(response.body().getStatus().equalsIgnoreCase("1")){
//                                        showMessage(response.body().getMessage());
//                                    } else {
//                                        showMessage(response.body().getMessage());
//                                    }
//                                }
//
//
//                            }
//
//                            @Override
//                            public void onFailure(Call<ResponseModel>call, Throwable t) {
//                                // Log error here since request failed
//                                hideLoading();
//                                showMessage(t.getMessage());
//
//                            }
//                        });
//
//
//                    } catch (Exception e) {
//                        e.printStackTrace();
//
//                    }
//                }
//            }
//        });

        dialog.show();
    }



}
