package com.project.android.api;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.project.android.api.Util.NetworkUtils;
import com.project.android.api.model.SendOtpReq;
import com.project.android.api.model.SendOtpRes;

import com.project.android.api.webService.APIClient;
import com.project.android.api.webService.APIInterface;

import retrofit2.Call;
import retrofit2.Callback;

public class OtpActivity extends AppCompatActivity {
    EditText code,sendto;
    TextView print;
    Button send;
    ProgressDialog progressDialog;

    //امضا
    String header="BASIC APIKEY:AC26E25D-4ED7-442D-B5C3-78BE45199E16";


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

        initViews();
        Listeners();
    }

    private void initViews() {
        code= findViewById(R.id.code);
        sendto=  findViewById(R.id.sendto);
        send=findViewById(R.id.send);
        print= findViewById(R.id.print);
    }

    private void Listeners() {

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//اگر گوشی به اینترنت متصل بود فیلدهارا ارسال کن
                if (NetworkUtils.isConnectedToInternet(OtpActivity.this)) {
                    if (code.getText().equals("")||code.getText().equals(null))
                    {
                        Toast.makeText(getApplicationContext(),
                                "کد را وارد نمایید", Toast.LENGTH_LONG).show();
                    }
                    else
                    sendOtp(sendto.getText().toString(),code.getText().toString());
                }
                else {
                    Toast.makeText(getApplicationContext()
                            ,R.string.network_error, Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    //درخواست ارسال otp به سرور
    private void sendOtp(String to,String code){

            if (progressDialog == null) {
                progressDialog = new ProgressDialog(OtpActivity.this);
                progressDialog.setMessage(getString(R.string.loadingData));
                progressDialog.setCancelable(false);
                progressDialog.setCanceledOnTouchOutside(false);
                progressDialog.show();
            }
            APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);

            SendOtpReq req = new SendOtpReq();
            req.setMobile(to);
            req.setSmsCode(Integer.valueOf(code));
            Call<SendOtpRes> call = apiInterface.sendOtp(req, header);
            call.enqueue(new Callback<SendOtpRes>() {
                @Override
                public void onResponse(Call<SendOtpRes> call, retrofit2.Response<SendOtpRes> response) {
                    if(response.isSuccessful()) {

                        if (response != null) {
                            try {
                                if (progressDialog.isShowing()) {
                                    progressDialog.dismiss();
                                    progressDialog = null;
                                }
                            } catch (Exception exception) {
                                exception.printStackTrace();
                            }
                            print.setText(response.body().getRMessage());
                        }
                        else {
                            print.setText(R.string.error);
                        }
                    }else{
                        if (progressDialog.isShowing()) {
                            progressDialog.dismiss();
                            progressDialog = null;
                        }

                        print.setText("Authorization Failed");
                    }



                }

                @Override
                public void onFailure(Call<SendOtpRes> call, Throwable t) {

                    if (progressDialog.isShowing()) {
                        progressDialog.dismiss();
                        progressDialog = null;
                    }
                    Toast.makeText(getApplicationContext(),
                            R.string.error, Toast.LENGTH_LONG).show();
                    print.setText(String.valueOf(t));
                }

            });
        }


}
