package com.casic.dcms.widgets;

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

import androidx.appcompat.app.AlertDialog;

import com.casic.dcms.R;
import com.casic.dcms.mvp.presenter.impl.LightDetailPresenterImpl;
import com.casic.dcms.mvp.view.ILightDetailView;
import com.casic.dcms.utils.ToastHelper;

import org.json.JSONException;
import org.json.JSONObject;

public class LightDetailDialog extends AlertDialog implements ILightDetailView {

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

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

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

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

    private final String lampId;
    private LightDetailPresenterImpl detailPresenter;
    private TextView lightStateView, lightAddressView, lightStreetView, lightElecView, lightVolView, lightTimeView;

    private LightDetailDialog(Builder builder) {
        super(builder.context, R.style.DetailDialogStyle);
        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);

        detailPresenter = new LightDetailPresenterImpl(this);
        detailPresenter.onReadyRetrofitRequest(lampId);
    }

    @Override
    public void obtainLightDetailResult(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 {
                ToastHelper.showToast("操作失败，请重试", ToastHelper.ERROR);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (detailPresenter != null) {
            detailPresenter.disposeRetrofitRequest();
        }
    }
}
