package com.example.myapplication.model;

import android.location.Location;

import java.util.ArrayList;
import java.util.List;

// SharedDataManager.java
public class SharedDataManager {
    private static SharedDataManager instance;
    private String imageId ="-1";
    private String token="-1";
    private String user="-1";
    private String audioPath="-1";
    private String projectId="-1";
    private String turbineId ="-1";
    private String turbineName ="-1";
    private String partId ="-1";
    private String partName ="-1";
    private String chooseImageSource="-1";
    private boolean isRecording=false;
    private boolean isUploading=false; // 新增上传状态字段
    private Location location = null; // 新增位置字段
    private final List<DataChangeListener> listeners = new ArrayList<>();
    private String othermsg;
    public interface DataChangeListener {
        void onImageIdChanged(String newId);
        void onTokenChanged(String newToken);
        void onUserChanged(String newUser);
        void onAudioPathChanged(String newAudioPath);
        void onProjectIdChanged(String newProjectId);
        void onPartIdChanged(String newPartId);
        void onPartNameChanged(String newPartName);
        void onTurbineIdChanged(String newTurbineId);
        void onTurbineNameChanged(String newTurbineName);
        void onChooseImageSourceChanged(String newChooseImageSource);
        void onIsRecordingChanged(boolean newisRecording);
        void onUploadingStatusChanged(boolean isUploading); // 新增上传状态变化回调

        void onLocationChanged(Location newLocation); // 新增位置变化回调
        void onOtherMsgChanged(String msg); // 新增位置变化回调
    }

    private SharedDataManager() {}

    public static synchronized SharedDataManager getInstance() {
        if (instance == null) {
            instance = new SharedDataManager();
        }
        return instance;
    }
    public boolean getIsRecording(){return isRecording;}
    public String getImageId() {return imageId;}
    public String getUser() { return user; }
    public String getAudioPath() { return audioPath; }
    public String getProjectId() { return projectId; }
    public String getPartId() { return partId; }
    public String getChooseImageSource() { return chooseImageSource; }
    public String getToken() {return token;}
    public boolean getisUploading() {
        return isUploading;
    }
    public Location getLocation() {
        return location;
    }
   public  String getOthermsg()
   {
       return  othermsg;

   }

    public String getTurbineId() {
        return turbineId;
    }

    public  void setOthermsg(String othermsg)
   {
       this.othermsg=othermsg;
       notifyOtherMsgChanged(othermsg);
   }
    public void setLocation(Location location) {
        this.location = location;
        notifyLocationChanged(location);
    }
    public void setUploading(boolean uploading) {
        if (this.isUploading != uploading) {
            this.isUploading = uploading;
            notifyUploadingStatusChanged(uploading);
        }
    }
    public void setImageId(String imageId) {
        this.imageId = imageId;
        notifyImageIdChanged(imageId);
    }

    public void setUser(String user) {
        this.user = user;
        notifyUserChanged(user);
    }

    public void setAudioPath(String audioPath) {
        this.audioPath = audioPath;
        notifyAudioPathChanged(audioPath);
    }

    public void setProjectId(String projectId) {
        this.projectId = projectId;
        notifyProjectIdChanged(projectId);
    }

    public void setPartId(String partId) {
        this.partId = partId;
        notifyPartIdChanged(partId);
    }

    public void setPartName(String partName) {
        this.partName = partName;
        notifyPartNameChanged(partName);
    }

    public void setChooseImageSource(String chooseImageSource) {
        this.chooseImageSource = chooseImageSource;
        notifyChooseImageSourceChanged(chooseImageSource);
    }

    public void setToken(String token) {
        this.token = token;
        notifyTokenChanged(token);
    }
    public void setIsRecording(boolean isRecording) {
        this.isRecording =isRecording ;
        notifyIsRecordingChanged(isRecording);
    }

    public void setTurbineId(String turbineId) {
        this.turbineId = turbineId;
        notifyTurbineIdChanged(turbineId);
    }

    public void setTurbineName(String turbineName) {
        this.turbineName = turbineName;
        notifyTurbineNameChanged(turbineName);
    }

    public void addListener(DataChangeListener listener) {
        listeners.add(listener);
    }

    public void removeListener(DataChangeListener listener) {
        listeners.remove(listener);
    }

    private void notifyImageIdChanged(String newPath) {
        for (DataChangeListener listener : listeners) {
            listener.onImageIdChanged(newPath);
        }
    }

    private void notifyTokenChanged(String newToken) {
        for (DataChangeListener listener : listeners) {
            listener.onTokenChanged(newToken);
        }
    }
    private void notifyUserChanged(String newUser) {
        for (DataChangeListener listener : listeners) {
            listener.onUserChanged(newUser);
        }
    }

    private void notifyAudioPathChanged(String newAudioPath) {
        for (DataChangeListener listener : listeners) {
            listener.onAudioPathChanged(newAudioPath);
        }
    }

    private void notifyProjectIdChanged(String newProjectId) {
        for (DataChangeListener listener : listeners) {
            listener.onProjectIdChanged(newProjectId);
        }
    }

    private void notifyPartIdChanged(String newPartId) {
        for (DataChangeListener listener : listeners) {
            listener.onPartIdChanged(newPartId);
        }
    }

    private void notifyPartNameChanged(String newPartName) {
        for (DataChangeListener listener : listeners) {
            listener.onPartNameChanged(newPartName);
        }
    }

    private void notifyTurbineIdChanged(String newTurbineId) {
        for (DataChangeListener listener : listeners) {
            listener.onTurbineIdChanged(newTurbineId);
        }
    }
    private void notifyTurbineNameChanged(String newTurbineName) {
        for (DataChangeListener listener : listeners) {
            listener.onTurbineNameChanged(newTurbineName);
        }
    }

    private void notifyChooseImageSourceChanged(String newChooseImageSource) {
        for (DataChangeListener listener : listeners) {
            listener.onChooseImageSourceChanged(newChooseImageSource);
        }
    }

    private void notifyIsRecordingChanged(boolean newIsRecording) {
        for (DataChangeListener listener : listeners) {
            listener.onIsRecordingChanged(newIsRecording);
        }
    }
    private void notifyUploadingStatusChanged(boolean isUploading) {
        for (DataChangeListener listener : listeners) {
            listener.onUploadingStatusChanged(isUploading);
        }
    }
    private void notifyLocationChanged(Location newLocation) {
        for (DataChangeListener listener : listeners) {
            listener.onLocationChanged(newLocation);
        }
    }
    private void notifyOtherMsgChanged(String newothermsg) {
        for (DataChangeListener listener : listeners) {
            listener.onOtherMsgChanged(newothermsg);
        }
    }
}