package com.phlx.wool.ui.setting;

import android.os.Bundle;


import com.phlx.wool.BR;
import com.phlx.wool.Configs;
import com.phlx.wool.R;
import com.phlx.wool.data.VMFactory;
import com.phlx.wool.databinding.ActivitySettingBinding;
import com.phlx.wool.net.RetrofitClient;
import com.phlx.wool.rfid.ModuleManager;
import com.phlx.wool.ui.base.BaseActivity;
import com.phlx.wool.util.TextUtil;

import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * <pre>
 *     author : lei
 *     e-mail : 18600395998@163.com
 *     time   : 2019/05/23
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class SettingActivity extends BaseActivity<ActivitySettingBinding, SettingVM> {


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_setting;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public void initData() {
        setSupportActionBar(binding.include.toolbar);
        viewModel.initToolbar();
        binding.ipAddress.setIpAddress(SPUtils.getInstance().getString(Configs.SP_APP_IP,
                RetrofitClient.baseUrl));
        ModuleManager.openCom();
    }


    @Override
    public void initViewObservable() {

        viewModel.saveIpEvent.observe(this, aBoolean -> {
            SPUtils.getInstance().put(Configs.SP_APP_IP, binding.ipAddress.getIpAddress());
            if (!TextUtil.isSpace(binding.portEdit.getText().toString())) {
                SPUtils.getInstance().put(Configs.SP_APP_PORT,
                        binding.portEdit.getText().toString());
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //                Intent intent = getPackageManager()
            //                        .getLaunchIntentForPackage(getApplication()
            //                        .getPackageName());
            //                PendingIntent restartIntent = PendingIntent.getActivity
            //                (getApplicationContext(),
            //                        0, intent, 0);
            //                AlarmManager mgr = (AlarmManager) getSystemService(Context
            //                .ALARM_SERVICE);
            //                mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000,
            //                restartIntent); //
            //                // 1秒钟后重启应用
            //                AppManager.getAppManager().AppExit();
            VMFactory.getInstance(getApplication()).refreshRepository();
            ToastUtils.showShort("保存IP,端口成功");
        });

    }


}
