package info.nightscout.androidaps.plugins.PumpDanaR.Dialogs;

import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.interfaces.ProfileInterface;
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
import info.nightscout.androidaps.plugins.PumpDanaRKorean.DanaRKoreanPlugin;
import info.nightscout.androidaps.plugins.PumpDanaRv2.DanaRv2Plugin;
import info.nightscout.utils.DecimalFormatter;

/**
 * Created by mike on 10.07.2016.
 */
public class ProfileViewDialog extends DialogFragment {
    private static Logger log = LoggerFactory.getLogger(ProfileViewDialog.class);

    private static TextView noProfile;
    private static TextView units;
    private static TextView dia;
    private static TextView activeProfile;
    private static TextView ic;
    private static TextView isf;
    private static TextView basal;
    private static TextView target;

    private static Button refreshButton;

    Handler mHandler;
    static HandlerThread mHandlerThread;

    public ProfileViewDialog() {
        mHandlerThread = new HandlerThread(ProfileViewDialog.class.getSimpleName());
        mHandlerThread.start();

        mHandler = new Handler(mHandlerThread.getLooper());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.nsprofileviewer_fragment, container, false);

        noProfile = (TextView) layout.findViewById(R.id.profileview_noprofile);
        units = (TextView) layout.findViewById(R.id.profileview_units);
        dia = (TextView) layout.findViewById(R.id.profileview_dia);
        activeProfile = (TextView) layout.findViewById(R.id.profileview_activeprofile);
        ic = (TextView) layout.findViewById(R.id.profileview_ic);
        isf = (TextView) layout.findViewById(R.id.profileview_isf);
        basal = (TextView) layout.findViewById(R.id.profileview_basal);
        target = (TextView) layout.findViewById(R.id.profileview_target);
        refreshButton = (Button) layout.findViewById(R.id.profileview_reload);
        refreshButton.setVisibility(View.VISIBLE);
        refreshButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        DanaRPump.getInstance().lastSettingsRead = new Date(0);
                        if (MainApp.getSpecificPlugin(DanaRPlugin.class).isEnabled(PluginBase.PUMP))
                            DanaRPlugin.doConnect("ProfileViewDialog");
                        if (MainApp.getSpecificPlugin(DanaRKoreanPlugin.class).isEnabled(PluginBase.PUMP))
                            DanaRKoreanPlugin.doConnect("ProfileViewDialog");
                        if (MainApp.getSpecificPlugin(DanaRv2Plugin.class).isEnabled(PluginBase.PUMP))
                            DanaRv2Plugin.doConnect("ProfileViewDialog");
                    }
                });
                dismiss();
            }
        });

        setContent();
        return layout;
    }

    @Override
    public void onResume() {
        super.onResume();
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    private void setContent() {
//        if (profile == null) {
//            noProfile.setVisibility(View.VISIBLE);
//            return;
//        } else {
//            noProfile.setVisibility(View.GONE);
//        }
        Profile profile = ((ProfileInterface)MainApp.getConfigBuilder().getActivePump()).getProfile().getDefaultProfile();
        units.setText(profile.getUnits());
        dia.setText(DecimalFormatter.to2Decimal(profile.getDia()) + " h");
        activeProfile.setText(((ProfileInterface)MainApp.getConfigBuilder().getActivePump()).getProfileName());
        ic.setText(profile.getIcList());
        isf.setText(profile.getIsfList());
        basal.setText(profile.getBasalList());
        target.setText(profile.getTargetList());
    }


}

@@ -506,16 +506,69 @@
 daps.R;%0A
+import info.nightscout.androidaps.data.ProfileStore;%0A
 import i
@@ -4247,15 +4247,18 @@
 file
- profil
+Store stor
 e = 
@@ -4332,16 +4332,130 @@
 rofile()
+;%0A        if (store != null) %7B%0A            noProfile.setVisibility(View.GONE);%0A            Profile profile = store
 .getDefa
@@ -4460,32 +4460,36 @@
 faultProfile();%0A
+    
         units.se
@@ -4523,16 +4523,20 @@
         
+ 
+   
 dia.setT
@@ -4594,32 +4594,36 @@
  %22 h%22);%0A        
+    
 activeProfile.se
@@ -4639,32 +4639,33 @@
 rofileInterface)
+ 
 MainApp.getConfi
@@ -4719,16 +4719,20 @@
         
+    
 ic.setTe
@@ -4764,16 +4764,20 @@
         
+   
+ 
 isf.setT
@@ -4795,32 +4795,36 @@
 .getIsfList());%0A
+    
         basal.se
@@ -4858,24 +4858,28 @@
 ));%0A        
+   
+ 
 target.setTe
@@ -4903,22 +4903,100 @@
 getList());%0A
+        %7D else %7B%0A            noProfile.setVisibility(View.VISIBLE);%0A        %7D%0A
     %7D%0A%0A%0A%7D%0A

fix crash when no profile loaded from pump yet