package com.miraclegarden.smsmessage.Activity;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.PermissionInfo;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.miraclegarden.library.app.MiracleGardenActivity;
import com.miraclegarden.smsmessage.databinding.ActivityMainBinding;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends MiracleGardenActivity<ActivityMainBinding> {
    public static SharedPreferences sp;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sp = getSharedPreferences("server", MODE_PRIVATE);

        initData();
        initView();
    }

    private void initData() {
        if (sp == null) {
            return;
        }
        binding.host.setText(sp.getString("host", ""));

    }

    private void initView() {
        binding.yes.setOnClickListener(v -> {
            if (sp == null) {
                return;
            }

            binding.host.setText(sp.getString("host", "https://www.judy88.xin/api/bills/app-upload"));

            if (binding.host.getText().toString().length() == 0) {
                Toast.makeText(this, "服务器和添加参数不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!binding.host.getText().toString().startsWith("http")) {
                Toast.makeText(this, "服务器地址错误", Toast.LENGTH_SHORT).show();
                return;
            }
            SharedPreferences.Editor edit = sp.edit();
            edit.putString("host", binding.host.getText().toString());
            edit.apply();
            startActivity(new Intent(MainActivity.this, NotificationActivity.class));
            finish();
        });
        new Handler().postDelayed(() -> binding.yes.performClick(),2000);
    }

}
