package com.example.bpa.view.fragment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.bpa.R;
import com.example.bpa.view.fragment.setting.mqttparameter;
import com.example.bpa.view.fragment.setting.ovarparameter;
import com.example.bpa.view.fragment.setting.systemparameter;

/**
 * 系统设置界面
 */
public class SystemSetFragment extends Fragment implements View.OnClickListener {


    View view;
    private Button btn_system_set,btn_mqtt_set,btn_over_set;
    private Fragment Systemparameter;//系统设置
    private Fragment mqtt;//mqtt设置
    private Fragment overparameter;//其他设置
    private Fragment SelectFragment;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_system_set, container, false);
        Init();
        initEvent();
        return view;

    }

    private void Init() {
        overparameter = new ovarparameter();
        mqtt=new mqttparameter();
        Systemparameter = new systemparameter();
        btn_system_set = view.findViewById(R.id.stparameter);
        //btn_mqtt_set = view.findViewById(R.id.mqttparameter);
        btn_over_set = view.findViewById(R.id.overparameter);
        //默认打开参数设置界面
        ShowFragment(Systemparameter);
    }

    private void initEvent() {
        btn_system_set.setOnClickListener(this);
        //btn_mqtt_set.setOnClickListener(this);
        btn_over_set.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.stparameter://系統參數設置
                ShowFragment(Systemparameter);
                break;
//            case R.id.mqttparameter://MQTT配置
//                ShowFragment(mqtt);
//                break;
            case R.id.overparameter://其他设置
                ShowFragment(overparameter);
                break;
        }
    }


    public void ShowFragment(Fragment fragment) {
        FragmentManager manager = getChildFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        if (SelectFragment != null) {
            transaction.remove(SelectFragment);
        }
        transaction.add(R.id.fragment_container, fragment);
        transaction.commit();
        SelectFragment = fragment;
    }
}