package com.usai.redant.raimage;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Switch;

public class uploadSettingActivity extends AppCompatActivity {

    private Switch auto_start_swt;
    private Switch auto_rm_fs_swt;
    private Switch auto_rm_er_swt;
    private Switch compress_swt;
    private EditText retry_count_et;

    public void initSetting() {
        boolean auto_upload = true;
        boolean auto_rm_fs = true;
        boolean auto_rm_er = false;
        boolean compress = true;
        int retry_count = 2;

        SharedPreferences preferences = getSharedPreferences("uploadSetting", Context.MODE_PRIVATE);
        if (preferences != null) {

            auto_upload = preferences.getBoolean("auto_upload",true);
            auto_rm_fs = preferences.getBoolean("auto_rm_fs",true);
            auto_rm_er = preferences.getBoolean("auto_rm_er",false);
            compress = preferences.getBoolean("compress",true);
            retry_count = preferences.getInt("retry_count",2);

        } else  {

        }


        auto_start_swt.setChecked(auto_upload);
        auto_rm_fs_swt.setChecked(auto_rm_fs);
        auto_rm_er_swt.setChecked(auto_rm_er);
        compress_swt.setChecked(compress);
        retry_count_et.setText(String.valueOf(retry_count));
        // 移动光标到末尾
        retry_count_et.setSelection(retry_count_et.getText().length());
    }

    public void saveSetting() {

        SharedPreferences preferences = getSharedPreferences("uploadSetting",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();

        boolean auto_upload = auto_start_swt.isChecked();
        boolean auto_rm_fs = auto_rm_fs_swt.isChecked();
        boolean auto_rm_er = auto_rm_er_swt.isChecked();
        boolean compress = compress_swt.isChecked();
        int retry_count = Integer.parseInt(retry_count_et.getText().toString());

        editor.putBoolean("auto_upload",auto_upload);
        editor.putBoolean("auto_rm_fs",auto_rm_fs);
        editor.putBoolean("auto_rm_er",auto_rm_er);
        editor.putBoolean("compress",compress);
        editor.putInt("retry_count",retry_count);

        editor.commit();
    }

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

        auto_start_swt = (Switch)findViewById(R.id.auto_start_swt);
        auto_rm_fs_swt = (Switch)findViewById(R.id.auto_rm_fs_swt);
        auto_rm_er_swt = (Switch)findViewById(R.id.auto_rm_er_swt);
        compress_swt = (Switch)findViewById(R.id.compress_swt);
        retry_count_et = (EditText)findViewById(R.id.retry_et);

        initSetting();

        ActionBar mActionBar=getSupportActionBar();
        mActionBar.setHomeButtonEnabled(true);
        mActionBar.setDisplayHomeAsUpEnabled(true);
        mActionBar.setTitle("RA Image");
    }

    @Override
    public boolean onSupportNavigateUp() {
        return super.onSupportNavigateUp();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.upload_setting_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.save_upload_setting_item : {
                saveSetting();
                finish();
            }
            break;
            case android.R.id.home : {
                finish();
            }
            break;
        }
        return true;
    }
}
