package com.sipai.jspatrol.Equ.Equfrgment;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
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.Equ.EquCardActivity;
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;

/**
 * created by lvliming on 2020/10/22
 */
public class EquDetailFragment extends Fragment {
    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,tv_abc,tv_type,tv_assetnumber,tv_equstatus;
    private EquList equ;
    private RecyclerView rc_view;
    private LinearLayoutManager manager;
    private MeasurePointsAdapter pointsAdapter;
    private List<Measuringpoint> measuringpoints = new ArrayList<>();
    private String mBizid;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_equ_detail,container,false);
        init();
        initView(view);
//        getEquPoints();
        return view;
    }

    private void init() {
        EquCardActivity equcard = (EquCardActivity) getActivity();
        equ = equcard.getEquList();
        sharedpre = new SharedPreferencesData(getActivity());
        map_base = sharedpre.getAll();
        mServer = map_base.get("mServer");
        SharedPreferences sharedPre = getActivity().getSharedPreferences("config", 0);
        mBizid = sharedPre.getString("bizid", "");
    }

    private void initView(View view) {
        tv_name = view.findViewById(R.id.moudle_equ_detail_tv_equipmentname);
        tv_name.setText(equ.getEquipmentname());
        tv_equstatus = view.findViewById(R.id.moudle_equ_detail_tv_equstatus);
        tv_equstatus.setText(equ.getEquipmentStatusManagement());
        tv_areaid = view.findViewById(R.id.moudle_equ_detail_tv_areaid);
        tv_areaid.setText(equ.getAreaid());
        tv_abc = view.findViewById(R.id.moudle_equ_detail_tv_abc);
        tv_abc.setText(equ.getEquipmentLevel());
        tv_assetnumber = view.findViewById(R.id.moudle_equ_detail_tv_assetnumber);
        tv_assetnumber.setText(equ.getAssetnumber());
        tv_equcardid = view.findViewById(R.id.moudle_equ_detail_tv_equipmentcardid);
        tv_equcardid.setText(equ.getEquipmentcardid());
        tv_equtype = view.findViewById(R.id.moudle_equ_detail_tv_equtype);
        tv_equtype.setText(equ.getEquipmentTypeNumber());
        tv_ps = view.findViewById(R.id.moudle_equ_detail_tv_ps);
        tv_ps.setText(equ.getProcessSection());
        tv_equmf = view.findViewById(R.id.moudle_equ_detail_tv_equmf);
        tv_equmf.setText(equ.getEquipmentmanufacturer());
        tv_company = view.findViewById(R.id.moudle_equ_detail_tv_company);
        tv_company.setText(equ.getCompany());
        tv_type = view.findViewById(R.id.moudle_equ_detail_tv_type);
        tv_type.setText(equ.getEquipmentClass());
        rc_view = view.findViewById(R.id.moudle_equ_detail_rc_point);
        manager = new LinearLayoutManager(getActivity());
        rc_view.setLayoutManager(manager);
    }

    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 (!getActivity().isFinishing()){
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    pointsAdapter = new MeasurePointsAdapter(getActivity(),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;
    }

}
