package com.xypower.frpandroid;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.xypower.common.FilesUtils;

import org.jetbrains.annotations.Nullable;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;

public class ConfigActivity extends AppCompatActivity {

    private String mConfigFilePath;

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

        Button saveConfigButton = (Button)this.findViewById(R.id.saveConfigButton);
        saveConfigButton.setOnClickListener((View.OnClickListener)(new View.OnClickListener() {
            public final void onClick(View it) {
                ConfigActivity.this.saveConfig();
                ConfigActivity.this.finish();
            }
        }));
        Button dontSaveConfigButton = (Button)this.findViewById(R.id.dontSaveConfigButton);
        dontSaveConfigButton.setOnClickListener((View.OnClickListener)(new View.OnClickListener() {
            public final void onClick(View it) {
                ConfigActivity.this.finish();
            }
        }));
        this.readConfig();
    }

    public final void readConfig() {
        String[] files = this.fileList();
        if (files == null) {
            return;
        }

        EditText configEditText = (EditText)this.findViewById(R.id.configEditText);
        configEditText.setText((CharSequence)"");

        Intent intent = getIntent();

        String server = intent.getStringExtra("server");
        if (!TextUtils.isEmpty(server)) {
            server = MainActivity.DEFAULT_SERVER;
        }
        String proxyName = intent.getStringExtra("proxyName");
        if (!TextUtils.isEmpty(proxyName)) {
            proxyName = Long.toString(System.currentTimeMillis() / 1000);
        }
        int port = intent.getIntExtra("port", MainActivity.DEFAULT_PORT);
        int remotePort = intent.getIntExtra("remotePort", MainActivity.DEFAULT_REMOTE_PORT);

        File file = MainActivity.checkConfig(getApplicationContext(), server, port, remotePort, proxyName);
        if (file != null) {
            mConfigFilePath = file.getAbsolutePath();

            String content = FilesUtils.readTextFile(mConfigFilePath);
            configEditText.setText(TextUtils.isEmpty(content) ? "" : content);
        }
    }


    public final void saveConfig() {
        EditText configEditText = (EditText)this.findViewById(R.id.configEditText);

        if (mConfigFilePath != null) {
            FilesUtils.writeTextFile(mConfigFilePath, configEditText.getText().toString());
        }
    }
}