package com.usai.redant.raimage;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Switch;

import com.usai.util.RAUploadManager;

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 Switch sw_wifiOnly;
    private EditText retry_count_et;
    private EditText retry_waiting_et;

    private ServiceConnection serviceConnection;
    private boolean serviceConnectionFlag = false; // 绑定服务标识
    private UploadService.MyBinder binder;
    private UploadService uploadServiceservice;

    public void initSetting() {
//        boolean auto_upload = true;
//        boolean auto_rm_fs = true;
//        boolean auto_rm_er = false;
        boolean compress = true;

        boolean wifionly = false;
        int retry_count = 5;

        int retry_waiting = 300;
//        SharedPreferences preferences = getSharedPreferences("uploadSetting", Context.MODE_PRIVATE);
        SharedPreferences preferences = RedAntApplication.getInstance().getSharedPreferences("UploadManager", 0);
        if (preferences != null) {

//            auto_upload = preferences.getBoolean("auto_upload",true);
//            auto_rm_fs = preferences.getBoolean("auto_rm_finish",true);
//            auto_rm_er = preferences.getBoolean("auto_rm_error",false);

            wifionly = preferences.getBoolean("wifi_only",false);
            compress = preferences.getBoolean("compress",true);
            retry_count = preferences.getInt("retry_count",5);
            retry_waiting = preferences.getInt("retry_waiting",300);

        } else  {

        }


        sw_wifiOnly.setChecked(wifionly);
//        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());

        retry_waiting_et.setText(String.valueOf(retry_waiting));
        // 移动光标到末尾
        retry_waiting_et.setSelection(retry_waiting_et.getText().length());
    }

    /** Service */
    private void bindService() {
        Intent intent = new Intent(uploadSettingActivity.this, UploadService.class);

        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    public void saveSetting() {

//        SharedPreferences preferences = getSharedPreferences("uploadSetting",Context.MODE_PRIVATE);
        SharedPreferences preferences = RedAntApplication.getInstance().getSharedPreferences("UploadManager", 0);
        SharedPreferences.Editor editor = preferences.edit();

//        final boolean auto_upload = auto_start_swt.isChecked();
//        final boolean auto_rm_fs = auto_rm_fs_swt.isChecked();
//        final boolean auto_rm_er = auto_rm_er_swt.isChecked();

        final boolean wifi_only = sw_wifiOnly.isChecked();
        boolean compress = compress_swt.isChecked();
        final int retry_count = Integer.parseInt(retry_count_et.getText().toString());
        final int retry_waiting = Integer.parseInt(retry_waiting_et.getText().toString());

//        editor.putBoolean("auto_upload",auto_upload);
//        editor.putBoolean("auto_rm_finish",auto_rm_fs);
//        editor.putBoolean("auto_rm_error",auto_rm_er);
        editor.putBoolean("compress",compress);
        editor.putInt("retry_count",retry_count);
        editor.putInt("retry_waiting",retry_waiting);
        editor.putBoolean("wifi_only",wifi_only);

        editor.commit();

        if (serviceConnectionFlag == true && uploadServiceservice != null) {
            RAUploadManager uploadManager = uploadServiceservice.uploadManager;
//            uploadManager.autoStart = auto_upload;
//            uploadManager.removeFinish = auto_rm_fs;
//            uploadManager.removeError = auto_rm_er;
            uploadManager.maxRetry = retry_count;
            uploadManager.retryWaiting = retry_waiting;
            uploadManager.wifiOnly = wifi_only;
        }

    }

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

        // 同时修改 Upload Manager
        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                serviceConnectionFlag = true;

                binder = (UploadService.MyBinder)service;
                uploadServiceservice = binder.getService();

            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                serviceConnectionFlag = false;
            }
        };

        bindService();


        sw_wifiOnly = (Switch)findViewById(R.id.sw_wifyonly);
//        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);
        retry_waiting_et = (EditText)findViewById(R.id.waiting_et);

        initSetting();

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

    @Override
    protected void onDestroy() {
        unbindService(serviceConnection);
        super.onDestroy();
    }

    @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;
    }
}
