package com.casic.dcms.widgets;

import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.LifecycleOwner;

import com.casic.dcms.R;

public class LightDetailDialog extends AlertDialog {

    public static class Builder {
        private Context context;
        private LifecycleOwner lifecycleOwner;
        private String lampId;

        public Builder setContext(Context context) {
            this.context = context;
            return this;
        }

        public Builder setLifecycleOwner(LifecycleOwner lifecycleOwner) {
            this.lifecycleOwner = lifecycleOwner;
            return this;
        }

        public Builder setStreetLightId(String id) {
            this.lampId = id;
            return this;
        }

        public LightDetailDialog build() {
            return new LightDetailDialog(this);
        }
    }

    private final Context context;
    private final LifecycleOwner lifecycleOwner;
    private final String lampId;
    private TextView lightStateView, lightAddressView, lightStreetView, lightElecView, lightVolView, lightTimeView;

    private LightDetailDialog(Builder builder) {
        super(builder.context, R.style.DetailDialogStyle);
        this.context = builder.context;
        this.lifecycleOwner = builder.lifecycleOwner;
        this.lampId = builder.lampId;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_light_detail);
        setCanceledOnTouchOutside(true);
        setCancelable(true);

        lightStateView = findViewById(R.id.lightStateView);
        lightAddressView = findViewById(R.id.lightAddressView);
        lightStreetView = findViewById(R.id.lightStreetView);
        lightElecView = findViewById(R.id.lightElecView);
        lightVolView = findViewById(R.id.lightVolView);
        lightTimeView = findViewById(R.id.lightTimeView);

//        LightViewModel lightViewModel = new ViewModelProvider(context).get(LightViewModel.class);
//        lightViewModel.obtainLightDetail(lampId);
//        lightViewModel.lightDetailModel.observe(lifecycleOwner, new Observer<String>() {
//            @Override
//            public void onChanged(String resultJson) {
//                try {
//                    JSONObject jsonObject = new JSONObject(resultJson);
//                    int code = jsonObject.getInt("code");
//                    if (code == 200) {
//                        JSONObject dataObject = jsonObject.getJSONObject("data");
//
//                        lightStateView.setText(dataObject.getString("statusName"));
//                        lightAddressView.setText(dataObject.getString("address"));
//                        lightStreetView.setText(dataObject.getString("streetName"));
//                        lightElecView.setText(String.valueOf(dataObject.getDouble("latestElec")));
//                        lightVolView.setText(String.valueOf(dataObject.getDouble("latestVol")));
//                        lightTimeView.setText(dataObject.getString("latestTime"));
//                    } else {
//                        StringHub.show(context, "操作失败，请重试");
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
    }
}
