package com.pixelnx.localhelp.ui.signup;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;

import android.view.View;
import android.widget.Toast;

import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.common.Priority;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.ParsedRequestListener;
import com.pixelnx.localhelp.R;
import com.pixelnx.localhelp.model.ModelUserData;
import com.pixelnx.localhelp.ui.vendor.ActivityAllStore;
import com.pixelnx.localhelp.ui.vendor.ActivityVandorDashBoard;
import com.pixelnx.localhelp.utils.Consts;
import com.pixelnx.localhelp.utils.sharedpref.SharedPref;
import com.pixelnx.localhelp.utils.widgets.CustomButton;
import com.pixelnx.localhelp.utils.widgets.CustomEditText;

public class ActivityOtp extends Activity {
    CustomEditText etOne;
    CustomEditText  etTwo;
    CustomEditText  etThree;
    CustomEditText  etFour;
    CustomEditText  mobile;
    CustomButton btContinue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_otp);

        mobile = findViewById(R.id.mbl);
        if(getIntent().hasExtra("mbl")){
            mobile.setText(""+getIntent().getStringExtra("mbl"));
        }
        etOne = findViewById(R.id.etOne);
        etTwo = findViewById(R.id.etTwo);
        etThree = findViewById(R.id.etThree);
        etFour = findViewById(R.id.etFour);
        btContinue = findViewById(R.id.btConti);

        etOne.addTextChangedListener(new GenericTextWatcher(etOne));
        etTwo.addTextChangedListener(new GenericTextWatcher(etTwo));
        etThree.addTextChangedListener(new GenericTextWatcher(etThree));
        etFour.addTextChangedListener(new GenericTextWatcher(etFour));

        btContinue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String password = etOne.getText().toString() + etTwo.getText().toString()
                        + etThree.getText().toString() + etFour.getText().toString();
                if (password.length() == 4) {

                   apiOtpVerify(password);


                }else{
                    Toast.makeText(ActivityOtp.this,"Enter OTP!",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    void apiOtpVerify(String password){
        AndroidNetworking.post(Consts.Status.BASE_URL + Consts.Status.VERIFY_MBL_API)
                .addBodyParameter("verify_code", password)
                .setPriority(Priority.IMMEDIATE)
                .build()
                .getAsObject(ModelUserData.class, new ParsedRequestListener<ModelUserData>() {
                    @Override
                    public void onResponse(ModelUserData response) {

if(response.getStatus().equalsIgnoreCase("1")) {
    ModelUserData modelUserData;
    SharedPref sharedPref = SharedPref.getInstance(ActivityOtp.this);
    sharedPref.setBooleanValue(Consts.Status.IS_REGIS, true);
    SharedPref.setUser(Consts.Status.SP, response);
    modelUserData = sharedPref.getUser(Consts.Status.SP);

    if (modelUserData.getData().getUserType().equalsIgnoreCase("3")) {
        Intent intent = new Intent(ActivityOtp.this, ActivityVandorDashBoard.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    } else {
        Intent intent = new Intent(ActivityOtp.this, ActivityAllStore.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
    Toast.makeText(ActivityOtp.this, "" + response.getMsg(), Toast.LENGTH_SHORT).show();
}else{
    Toast.makeText(ActivityOtp.this, "" + response.getMsg(), Toast.LENGTH_SHORT).show();
}
                    }

                    @Override
                    public void onError(ANError anError) {
                        Toast.makeText(ActivityOtp.this,""+anError,Toast.LENGTH_SHORT).show();
                    }
                });




    }

    public class GenericTextWatcher implements TextWatcher {

        private View view;

        private GenericTextWatcher(View view) {
            this.view = view;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            String text = editable.toString();
            switch (view.getId()) {

                case R.id.etOne:
                    if (text.length() == 1) {
                        etTwo.requestFocus();
                    }
                    break;

                case R.id.etTwo:
                    if (text.length() == 1) {
                        etThree.requestFocus();
                    } else {
                        etOne.requestFocus();
                    }
                    break;

                case R.id.etThree:
                    if (text.length() == 1) {
                        etFour.requestFocus();
                    } else {
                        etTwo.requestFocus();
                    }
                    break;

                case R.id.etFour:
                    if (text.length() == 0) {
                        etThree.requestFocus();
                    }
                    break;
            }

        }
    }
}
