package kr.co.icontrols.iotplatform;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

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

import java.io.IOException;

import kr.co.icontrols.iotplatform.DataClass.UserInfo;
import kr.co.icontrols.iotplatform.utils.HttpApi;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import static android.content.Intent.EXTRA_PHONE_NUMBER;
import static kr.co.icontrols.iotplatform.utils.CommonUtils.DismissConnectDialog;
import static kr.co.icontrols.iotplatform.utils.CommonUtils.ErrorLogOutput;
import static kr.co.icontrols.iotplatform.utils.CommonUtils.WarnLogOutput;
import static kr.co.icontrols.iotplatform.utils.CommonUtils.displayProgress;
import static kr.co.icontrols.iotplatform.utils.CommonUtils.setupUI;
import static kr.co.icontrols.iotplatform.utils.CommonUtils.showNotiMsg;

public class PhoneNumChangeActivity extends AppCompatActivity implements View.OnClickListener, TextWatcher {

    private String TAG = "PhoneNumChangeActivity";
    private EditText txtPhoneNum;

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

        setupUI(this,findViewById(R.id.layoutMain));

        txtPhoneNum = findViewById(R.id.txtPhoneNum);
        txtPhoneNum.addTextChangedListener(this);
        txtPhoneNum.setText(getIntent().getStringExtra(EXTRA_PHONE_NUMBER));
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        /*
        final View fv = v;
        fv.setEnabled(false);
        Timer buttonTimer = new Timer();
        buttonTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            fv.setEnabled(true);
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {

                        }
                    }
                });
            }
        }, 500);
        */

        switch (v.getId()) {
            case R.id.btnBack:
                onBackPressed();
                break;
            case R.id.btnOk:
                changePhoneNum(txtPhoneNum.getText().toString());
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if(txtPhoneNum.length() > 0) {
            findViewById(R.id.btnOk).setEnabled(true);
        } else {
            findViewById(R.id.btnOk).setEnabled(false);
        }
    }

    private void changePhoneNum(final String phoneNumber) {
        try {
            displayProgress(PhoneNumChangeActivity.this, "","");
            HttpApi.changeUserInfo("", "", phoneNumber, UserInfo.pushAccess, UserInfo.autoUpdate, "", new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                    ErrorLogOutput(TAG, "HttpApi.changeUserInfo(onFailure)\n" + e.toString());
                    showNotiMsg(PhoneNumChangeActivity.this, getString(R.string.snackbar_fail_change_hp));
                    DismissConnectDialog();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if(response.code() == HttpApi.HTTP_NO_CONTENT) {
                        WarnLogOutput(TAG, "HttpApi.changeUserInfo(onResponse) HTTP_NO_CONTENT");
                        UserInfo.phoneNumber = phoneNumber;
                        showNotiMsg(PhoneNumChangeActivity.this, getString(R.string.snackbar_successed_change_hp),true);
                    } else {
                        ErrorLogOutput(TAG, "HttpApi.changeUserInfo(onResponse) Error code : " + response.code() + "\nresponse : " + response.toString());
                        try {
                            JSONObject json_data = new JSONObject(response.body().string());
                            ErrorLogOutput(TAG, "HttpApi.changeUserInfo(onResponse) body : " + json_data.toString());
                        } catch (JSONException e) {
                            ErrorLogOutput(TAG, "HttpApi.changeUserInfo(onResponse) JSONException : " + e.toString());
                        }
                        showNotiMsg(PhoneNumChangeActivity.this, getString(R.string.snackbar_fail_change_hp));
                    }
                    DismissConnectDialog();
                }
            });
        } catch (Exception e){
            e.printStackTrace();
            ErrorLogOutput(TAG, "HttpApi.changeUserInfo(Exception)\n" + e.toString());
            DismissConnectDialog();
            showNotiMsg(PhoneNumChangeActivity.this, getString(R.string.snackbar_fail_change_hp));
        }
    }
}