package com.olmatix.adapter;

/**
 * Created by Lesjaw on 04/12/2016.
 */

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.olmatix.helper.ItemTouchHelperAdapter;
import com.olmatix.model.Installed_NodeModel;
import com.olmatix.lesjaw.olmatix.R;
import com.olmatix.ui.fragment.Installed_Node;
import com.olmatix.utils.Connection;

import org.eclipse.paho.client.mqttv3.MqttException;

import java.util.Collections;
import java.util.List;

public class OlmatixAdapter extends RecyclerView.Adapter<OlmatixAdapter.OlmatixHolder>  implements ItemTouchHelperAdapter {

    List<Installed_NodeModel> nodeList;



    public class OlmatixHolder extends RecyclerView.ViewHolder {
        public TextView fwName, ipAddrs, upTime, siGnal, nodeid;
        public ImageView imgNode, imgStatus;

        public OlmatixHolder(View view) {
            super(view);
            imgNode     = (ImageView) view.findViewById(R.id.icon_node);
            imgStatus   = (ImageView) view.findViewById(R.id.icon_status);
            fwName    = (TextView) view.findViewById(R.id.fw_name);
            ipAddrs     = (TextView) view.findViewById(R.id.ipaddrs);
            siGnal      = (TextView) view.findViewById(R.id.signal);
            upTime      = (TextView) view.findViewById(R.id.uptime);
            nodeid      = (TextView) view.findViewById(R.id.nodeid);
        }
    }

    public OlmatixAdapter(List<Installed_NodeModel> nodeList) {
        this.nodeList = nodeList;
    }



    @Override
    public OlmatixHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.frag_nodeitemlist, parent, false);

        return new OlmatixHolder(itemView);
    }

    @Override
    public void onBindViewHolder(OlmatixHolder holder, int position) {

        final Installed_NodeModel mInstalledNodeModel = nodeList.get(position);
        if(mInstalledNodeModel.getOnline() != null) {
            if (mInstalledNodeModel.getOnline().equals("true")) {
                holder.imgStatus.setImageResource(R.drawable.ic_node_online);
            } else {
                holder.imgStatus.setImageResource(R.drawable.ic_node_offline);
            }
        }
        holder.imgNode.setImageResource(R.drawable.olmatixlogo);
        if(mInstalledNodeModel.getFwName() != null) {
            if (mInstalledNodeModel.getFwName().equals("smartfitting")) {
                holder.imgNode.setImageResource(R.mipmap.ic_light);
            } else if (mInstalledNodeModel.getFwName().equals("smartadapter4ch")){
                holder.imgNode.setImageResource(R.mipmap.ic_adapter);
            }
        }
        holder.fwName.setText(mInstalledNodeModel.getFwName());
        holder.ipAddrs.setText("IP : "+mInstalledNodeModel.getLocalip());
        holder.siGnal.setText("Signal : "+mInstalledNodeModel.getSignal()+"%");
        holder.upTime.setText("Uptime : "+mInstalledNodeModel.getUptime());
        holder.nodeid.setText(mInstalledNodeModel.getNodesID());
    }

    @Override
    public int getItemCount() {
        return nodeList.size();
    }

    public void removeItem(int position) {
        //Installed_Node.dbNodeRepo.delete("809ed5e0");
        String inputResult;
        Installed_Node.dbNodeRepo.delete(nodeList.get(position).getNodesID());
        String topic = "devices/"+nodeList.get(position).getNodesID()+"/#";
        try {
            Connection.getClient().unsubscribe(topic);
        } catch (MqttException e) {
            e.printStackTrace();
        }
        String topic1 = "devices/"+nodeList.get(position).getNodesID()+"/$online";
        try {
            Connection.getClient().unsubscribe(topic1);
        } catch (MqttException e) {
            e.printStackTrace();
        }
        nodeList.remove(position);

        notifyItemRemoved(position);
        notifyItemRangeChanged(position, nodeList.size());

    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        Collections.swap(nodeList, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
        return true;
    }

    @Override
    public void onItemDismiss(int position) {

    }

}
@@ -111,24 +111,49 @@
 cyclerView;%0A
+import android.util.Log;%0A
 import andro
@@ -3071,24 +3071,201 @@
 nal()+%22%25%22);%0A
+        long seconds = Long.parseLong(mInstalledNodeModel.getUptime());%0A        calculateTime(seconds);%0A        //Log.d(%22DEBUG%22, %22onBindViewHolder: %22 + calculateTime(updSec));%0A%0A
         hold
@@ -3298,38 +3298,29 @@
 : %22+
-mInstalledNodeModel.getUptime(
+calculateTime(seconds
 ));%0A
@@ -3383,32 +3383,724 @@
 esID());%0A    %7D%0A%0A
+    public static String calculateTime(long seconds) %7B%0A        Log.d(%22DEBUG%22, %22calculateTime: %22 + seconds);%0A        long sec = seconds %25 60;%0A        long minutes = seconds %25 3600 / 60;%0A        long hours = seconds %25 86400 / 3600;%0A        long days = seconds / 86400;%0A        String uptimeUpd;%0A        if(days!= 0)%7B%0A            uptimeUpd = days + %22 D %22 + hours + %22 H%22;%0A        %7D else if (hours != 0)%7B%0A            uptimeUpd = hours + %22 H %22 + minutes + %22 M%22;%0A        %7D else if (minutes != 0)%7B%0A            uptimeUpd = minutes + %22 M %22 + sec + %22 s%22;%0A        %7D else %7B%0A            uptimeUpd = sec + %22 s%22 ;%0A        %7D%0A%0A%0A               // System.out.println(uptimeUpd);%0A        return uptimeUpd;%0A    %7D%0A%0A
     @Override%0A  

change uptime format