package com.pixelnx.localhelp.ui.login;



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

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import android.widget.Toast;

import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.common.Priority;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.StringRequestListener;
import com.pixelnx.localhelp.R;

import com.pixelnx.localhelp.utils.Consts;
import com.pixelnx.localhelp.utils.ProjectUtils;
import com.pixelnx.localhelp.utils.widgets.CustomButton;
import com.pixelnx.localhelp.utils.widgets.CustomEditText;


import org.json.JSONException;
import org.json.JSONObject;

public class ActivityForgotPass extends Activity implements View.OnClickListener {
    CustomButton btContinue;
    CustomButton btConfirm;
    CustomEditText mobile;
    CustomEditText pass;
    ImageView show;
    ImageView hide;
    ImageView backIv;
    LinearLayout llNum;
    LinearLayout llOtp;
    CustomEditText etOne;
    CustomEditText etTwo;
    CustomEditText  etThree;
    CustomEditText  etFour;
    Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgot_pass);
        mContext=ActivityForgotPass.this;
        initial();
    }
    void initial(){
        btContinue=findViewById(R.id.btcontinue);
        btContinue.setOnClickListener(this);
        backIv=findViewById(R.id.backIv);
        llNum=findViewById(R.id.llNum);
        llOtp=findViewById(R.id.llotp);
        etOne = findViewById(R.id.etOne);
        etTwo = findViewById(R.id.etTwo);
        etThree = findViewById(R.id.etThree);
        etFour = findViewById(R.id.etFour);
        backIv.setOnClickListener(this);
        btConfirm=findViewById(R.id.btconfirm);
        btConfirm.setOnClickListener(this);
        mobile=findViewById(R.id.mbl);
        pass=findViewById(R.id.pass);
        show=findViewById(R.id.show);
        show.setOnClickListener(this);
        hide=findViewById(R.id.hide);
        hide.setOnClickListener(this);
        show.setVisibility(View.GONE);
        hide.setVisibility(View.VISIBLE);
        etOne.addTextChangedListener(new GenericTextWatcher(etOne));
        etTwo.addTextChangedListener(new GenericTextWatcher(etTwo));
        etThree.addTextChangedListener(new GenericTextWatcher(etThree));
        etFour.addTextChangedListener(new GenericTextWatcher(etFour));
    }
    void apiOtp(String otp){
        AndroidNetworking.post(Consts.Status.BASE_URL + "verify_mobile")
                .addBodyParameter("verify_code",""+otp)
                .addBodyParameter("new_password",pass.getText().toString())
                .setPriority(Priority.IMMEDIATE)
                .build()
                .getAsString(new StringRequestListener() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            JSONObject jsonObject=new JSONObject(response);
                            if(jsonObject.getString("status").equalsIgnoreCase("1")){
                                Intent intent=new Intent(ActivityForgotPass.this,ActivityLogin.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(intent);
                            }
                            Toast.makeText(ActivityForgotPass.this,""+jsonObject.getString("msg"),Toast.LENGTH_SHORT).show();

                        } catch (JSONException e) {

                            e.printStackTrace();
                        }
                        ProjectUtils.pauseProgressDialog();

                    }

                    @Override
                    public void onError(ANError anError) {
                        ProjectUtils.pauseProgressDialog();
                    }
                });
    }


    void api(){
        AndroidNetworking.post(Consts.Status.BASE_URL + Consts.Status.API_FORGOT_PASS)
                .addBodyParameter("u_mobile",mobile.getText().toString())
                .setPriority(Priority.IMMEDIATE)
                .build()
                .getAsString(new StringRequestListener() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            JSONObject jsonObject=new JSONObject(response);
                            if(jsonObject.getString("status").equalsIgnoreCase("1")){
                                llNum.setVisibility(View.GONE);
                                llOtp.setVisibility(View.VISIBLE);
                            }
                                Toast.makeText(ActivityForgotPass.this,""+jsonObject.getString("msg"),Toast.LENGTH_SHORT).show();

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        ProjectUtils.pauseProgressDialog();

                    }

                    @Override
                    public void onError(ANError anError) {
                        ProjectUtils.pauseProgressDialog();

                    }
                });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
        case R.id.show:

            show.setVisibility(View.GONE);
            hide.setVisibility(View.VISIBLE);
            pass.setInputType(InputType.TYPE_CLASS_TEXT |
                    InputType.TYPE_TEXT_VARIATION_PASSWORD);
            pass.setSelection(pass.getText().length());

            break;
            case R.id.hide:

                pass.setInputType(InputType.TYPE_CLASS_TEXT);
                hide.setVisibility(View.GONE);
                show.setVisibility(View.VISIBLE);
                pass.setSelection(pass.getText().length());
                break;

            case R.id.backIv:
                onBackPressed();
        break;


            case R.id.btconfirm:
                if(pass.getText().toString().length() > 5){
                    String password = etOne.getText().toString() + etTwo.getText().toString()
                            + etThree.getText().toString() + etFour.getText().toString();
                    if (password.length() == 4) {
                        ProjectUtils.showProgressDialog(mContext, false, "Loading...");
                    apiOtp(password);}else {
                        Toast.makeText(ActivityForgotPass.this,"OTP required",Toast.LENGTH_SHORT).show();
                    }
                }else{
                   pass.setError("Password must contain atleast 6 characters.");
                   pass.requestFocus();
                }
                break;
            case R.id.btcontinue:
                if(mobile.getText().toString().length() == 10){
                    ProjectUtils.showProgressDialog(mContext, false, "Loading...");
                    api();
                }else{
                    mobile.setError("Enter Valid Mobile Number.");
                    mobile.requestFocus();
                }
                break;

        }

    }
    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;
            }

        }
    }
}
