package com.navinfo.outdoor.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.navinfo.outdoor.R;
import com.navinfo.outdoor.api.Constant;
import com.navinfo.outdoor.base.BaseFragment;

/**
 * 设置页面的fragment
 */
public class SetFragment extends BaseFragment implements View.OnClickListener {

    private ImageView ivSet;
    private Switch switchTrafficRemind;
    private Switch switchInform;
    private Switch switchAutoSave;
    private Switch switchRealUpload;
    private SharedPreferences.Editor navInfoEditor;
    private SharedPreferences navInfo;

    public static SetFragment newInstance(Bundle bundle) {
        SetFragment fragment = new SetFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayout() {
        return R.layout.set_fragment;
    }

    @Override
    protected void initData() {
        super.initData(); }

    @Override
    protected void initView() {
        super.initView();
        navInfo = getActivity().getSharedPreferences(Constant.SHARED_PREFERENCES, Context.MODE_PRIVATE);
        navInfoEditor = getActivity().getSharedPreferences(Constant.SHARED_PREFERENCES, Context.MODE_PRIVATE).edit();
        ivSet = findViewById(R.id.iv_set);
        ivSet.setOnClickListener(this);
        switchRealUpload = findViewById(R.id.switch_real_upload);
        switchRealUpload.setChecked(navInfo.getBoolean("switchRealUpload", false));
        switchRealUpload.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                //控制开关字体颜色
                navInfoEditor.putBoolean("switchRealUpload", b);
                navInfoEditor.commit();
            }
        });
        switchAutoSave = findViewById(R.id.switch_autosave);
        switchAutoSave.setChecked(navInfo.getBoolean("switchAutoSave", false));
        switchAutoSave.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                //控制开关字体颜色
                navInfoEditor.putBoolean("switchAutoSave", b);
                navInfoEditor.commit();
            }
        });
        switchInform = findViewById(R.id.switch_inform);
        switchInform.setChecked(navInfo.getBoolean("switchInform", false));
        switchInform.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                //控制开关字体颜色
                navInfoEditor.putBoolean("switchInform", b);
                navInfoEditor.commit();
            }
        });
        switchTrafficRemind = findViewById(R.id.switch_traffic_remind);
        switchTrafficRemind.setChecked(navInfo.getBoolean("switchTrafficRemind", false));
        switchTrafficRemind.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                //控制开关字体颜色
                navInfoEditor.putBoolean("switchTrafficRemind", b);
                navInfoEditor.commit();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_set:
                getActivity().finish();
                break;
        }
    }
}
