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.SendSmsReq;
import com.project.android.api.model.SendSmsRes;
import com.project.android.api.webService.APIClient;
import com.project.android.api.webService.APIInterface;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;

public class SmsActivity extends AppCompatActivity {
    EditText message,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_sms);

            initViews();
            Listeners();
        }


    private void initViews() {

        message= findViewById(R.id.message);
        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(SmsActivity.this)) {
                    sendSms(sendto.getText().toString(),message.getText().toString());
                }
                else {
                    Toast.makeText(getApplicationContext()
                            ,R.string.network_error, Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    //ارسال پیامک از سرور
    private void sendSms(String to,String message){
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(SmsActivity.this);
            progressDialog.setMessage(getString(R.string.loadingData));
            progressDialog.setCancelable(false);
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.show();
        }

        APIInterface apiInterface= APIClient.getClient().create(APIInterface.class);
        List<String> numbers=new ArrayList<>();
        numbers.add(to);
        SendSmsReq req=new SendSmsReq();
        req.setMobiles(numbers);
        req.setSmsBody(message);

        Call<SendSmsRes> call=apiInterface.sendSms(req,header);
        call.enqueue(new Callback<SendSmsRes>() {
            @Override
            public void onResponse(Call<SendSmsRes> call, retrofit2.Response<SendSmsRes> 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<SendSmsRes> call, Throwable t) {

                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                    progressDialog = null;
                }

                Toast.makeText(getApplicationContext() ,
                        R.string.error, Toast.LENGTH_LONG).show();
                print.setText(t.getMessage().toString());
            }

        });

    }
}
