package kr.co.icontrols.iotplatform;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import kr.co.icontrols.iotplatform.Database.DBUtil;
import kr.co.icontrols.iotplatform.utils.SharedPrefUtil;

import static kr.co.icontrols.iotplatform.Database.DatabaseHelper.DATABASE_NAME;
import static kr.co.icontrols.iotplatform.PopupActivity.DEFAULT_POPUP_TIMEOUT;
import static kr.co.icontrols.iotplatform.utils.Constants.RESULT_INIT_APP_CONFIRM;
import static kr.co.icontrols.iotplatform.utils.Constants.RESULT_LOGOUT_SUCCESS;

public class AppInfoActivity extends AppCompatActivity implements View.OnClickListener {

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

        try {
            String currentVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
            ((TextView)findViewById(R.id.txtCurrentVersion)).setText(getString(R.string.current_version) + " " + currentVersion);
        } catch (PackageManager.NameNotFoundException e){
            e.printStackTrace();
        }

    }

    @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.btnInit:
                Intent intent = new Intent(this, PopupActivity.class);
                intent.putExtra(PopupActivity.POPUP_MODE, PopupActivity.MODE_YESNO);
                intent.putExtra(PopupActivity.POPUP_TITLE_TEXT, getString(R.string.popup_init_app_confirm));
                intent.putExtra(PopupActivity.POPUP_OK_BUTTON_TEXT, getString(R.string.popup_init_app_confirm_ok));
                intent.putExtra(PopupActivity.POPUP_CANCEL_BUTTON_TEXT, getString(R.string.no));
                startActivityForResult(intent, RESULT_INIT_APP_CONFIRM);
                break;
            case R.id.btnBack:
                onBackPressed();
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case RESULT_INIT_APP_CONFIRM:
                if (resultCode == Activity.RESULT_OK) {
                    SharedPrefUtil.reset();
                    DBUtil.removeAll();
                    deleteDatabase(DATABASE_NAME);
                    IotPlatformApplication.getInstance().clearApplicationData();

                    Intent intent = new Intent(this, PopupActivity.class);
                    intent.putExtra(PopupActivity.POPUP_MODE, PopupActivity.MODE_NORMAL);
                    intent.putExtra(PopupActivity.POPUP_BODY_TEXT, getString(R.string.popup_success_init_app));
                    intent.putExtra(PopupActivity.POPUP_TIMEOUT, DEFAULT_POPUP_TIMEOUT);
                    startActivityForResult(intent, RESULT_LOGOUT_SUCCESS);
                }
                break;
            case RESULT_LOGOUT_SUCCESS:
                if (resultCode == Activity.RESULT_OK) {
                    Intent intent = new Intent(this, SplashActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    finish();
                }
                break;
        }
    }
}