package com.sipai.jspatrol.Equ;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sipai.jspatrol.Bean.EquList;
import com.sipai.jspatrol.Bean.Measuringpoint;
import com.sipai.jspatrol.ProMonitoring.MeasurePointsAdapter;
import com.sipai.jspatrol.R;
import com.sipai.jspatrol.URL.MethodURL;
import com.sipai.jspatrol.utils.OKHttpUtils;
import com.sipai.jspatrol.utils.SharedPreferencesData;

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

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class EquDetailActivity extends AppCompatActivity {
    private SharedPreferencesData sharedpre;
    private HashMap<String, String> map_base = new HashMap<>();
    private String mServer;
    private TextView tv_name,tv_areaid,tv_equcardid,tv_equtype,tv_ps,tv_equmf,tv_company;
    private EquList equ;
    private RecyclerView rc_view;
    private LinearLayoutManager manager;
    private MeasurePointsAdapter pointsAdapter;
    private List<Measuringpoint> measuringpoints = new ArrayList<>();

    public static void actionStart(Context context, EquList equList){
        Intent intent = new Intent(context, EquDetailActivity.class);
        intent.putExtra("equ",equList);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_equ_detail);
        init();
        initView();
        getEquPoints();
    }

    private void getEquPoints() {
        HashMap<String,String> map = new HashMap<>();
        map.put("equipmentId",equ.getId());
        OKHttpUtils.getClient().newCall(OKHttpUtils.OKHttpRequest(map, mServer, MethodURL.GETEquPoints_Method)).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                String a = e.toString();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                measuringpoints = parseMeasuringPoints(res);
                if (measuringpoints.size()>0){
                    updatePoints();
                }
            }
        });
    }

    private void updatePoints() {
        if (!EquDetailActivity.this.isFinishing()){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    pointsAdapter = new MeasurePointsAdapter(EquDetailActivity.this,measuringpoints);
                    rc_view.setAdapter(pointsAdapter);
                }
            });
        }
    }

    private List<Measuringpoint> parseMeasuringPoints(String res) {
        List<Measuringpoint> points = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(res);
            JSONArray arrayPoint = jsonObject.getJSONArray("mPoint4APP");
            for (int a=0;a<arrayPoint.length();a++){
                Measuringpoint point = new Measuringpoint();
                point.setSid(arrayPoint.getJSONObject(a).opt("id").toString());
                point.setName(arrayPoint.getJSONObject(a).opt("parmname").toString());
                point.setUnit(arrayPoint.getJSONObject(a).opt("unit").toString());
                point.setCvalue(arrayPoint.getJSONObject(a).opt("parmvalue").toString());
                point.setType(arrayPoint.getJSONObject(a).opt("signalType").toString());
                point.setScadacode(arrayPoint.getJSONObject(a).opt("id").toString());
                point.setAlarmmax(arrayPoint.getJSONObject(a).opt("alarmmax").toString());
                point.setAlarmmin(arrayPoint.getJSONObject(a).opt("alarmmin").toString());
                points.add(point);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return points;
    }

    private void init() {
        if (getIntent().getSerializableExtra("equ")!=null){
            equ = (EquList) getIntent().getSerializableExtra("equ");
        }
        sharedpre = new SharedPreferencesData(EquDetailActivity.this);
        map_base = sharedpre.getAll();
        mServer = map_base.get("mServer");
    }

    private void initView() {
        TextView tv_title = findViewById(R.id.moudle_back_title_tv_title);
        tv_title.setText("设备详情");
        ImageView btn_back = findViewById(R.id.moudle_back_title_iv_back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_name = findViewById(R.id.moudle_equ_detail_tv_equipmentname);
        tv_name.setText(equ.getEquipmentname());
        tv_areaid = findViewById(R.id.moudle_equ_detail_tv_areaid);
        tv_areaid.setText(equ.getAreaid());
        tv_equcardid = findViewById(R.id.moudle_equ_detail_tv_equipmentcardid);
        tv_equcardid.setText(equ.getEquipmentcardid());
        tv_equtype = findViewById(R.id.moudle_equ_detail_tv_equtype);
        tv_equtype.setText(equ.getEquipmentTypeNumber());
        tv_ps = findViewById(R.id.moudle_equ_detail_tv_ps);
        tv_ps.setText(equ.getProcessSection());
        tv_equmf = findViewById(R.id.moudle_equ_detail_tv_equmf);
        tv_equmf.setText(equ.getEquipmentmanufacturer());
        tv_company = findViewById(R.id.moudle_equ_detail_tv_company);
        tv_company.setText(equ.getCompany());
        rc_view = findViewById(R.id.moudle_equ_detail_rc_point);
        manager = new LinearLayoutManager(EquDetailActivity.this);
        rc_view.setLayoutManager(manager);
    }
}
