package com.uxxu.konashi.test_all_functions;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.uxxu.konashi.lib.Konashi;
import com.uxxu.konashi.lib.KonashiManager;

import org.jdeferred.DoneCallback;

/**
 * Created by kiryu on 7/27/15.
 */
public final class KonashiInfoFragment extends Fragment {

    public static final String TITLE = "Konashi Info";

    private KonashiManager mKonashiManager;

    private TextView mNameTextView;
    private TextView mRssiTextView;
    private ProgressBar mRssiProgressBar;
    private TextView mBatteryTextView;
    private ProgressBar mBatteryProgressBar;
    private Button mReloadButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().setTitle(TITLE);
    }

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

        mNameTextView = (TextView) view.findViewById(R.id.nameTextView);

        mRssiTextView = (TextView) view.findViewById(R.id.rssiTextView);
        mRssiProgressBar = (ProgressBar) view.findViewById(R.id.rssiProgressBar);

        mBatteryTextView = (TextView) view.findViewById(R.id.batteryTextView);
        mBatteryProgressBar = (ProgressBar) view.findViewById(R.id.batteryProgressBar);

        mReloadButton = (Button) view.findViewById(R.id.reloadButton);
        mReloadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reload();
            }
        });

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mKonashiManager = Konashi.getManager();
        reload();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void reload() {
        if (!mKonashiManager.isReady()) {
            return;
        }

        mNameTextView.setText(mKonashiManager.getPeripheralName());

        mKonashiManager.getBatteryLevel().then(new DoneCallback<Integer>() {
            @Override
            public void onDone(final Integer result) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mBatteryTextView.setText(String.format("%d%%", result));
                        mBatteryProgressBar.setProgress(result);
                    }
                });
            }
        });
        mKonashiManager.getSignalStrength().then(new DoneCallback<Integer>() {
            @Override
            public void onDone(final Integer result) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mRssiTextView.setText(String.format("%ddb", result));
                        mRssiProgressBar.setProgress(Math.abs(result));
                    }
                });
            }
        });
    }
}

@@ -2498,147 +2498,8 @@
 ) %7B%0A
-                getActivity().runOnUiThread(new Runnable() %7B%0A                    @Override%0A                    public void run() %7B%0A        
     
@@ -2579,32 +2579,24 @@
             
-     
-   
 mBatteryProg
@@ -2628,50 +2628,8 @@
 t);%0A
-                    %7D%0A                %7D);%0A
     
@@ -2774,38 +2774,32 @@
 lic void onDone(
-final 
 Integer result) 
@@ -2804,147 +2804,8 @@
 ) %7B%0A
-                getActivity().runOnUiThread(new Runnable() %7B%0A                    @Override%0A                    public void run() %7B%0A        
     
@@ -2882,32 +2882,24 @@
             
-  
-      
 mRssiProgres
@@ -2938,50 +2938,8 @@
 ));%0A
-                    %7D%0A                %7D);%0A
     

Revert "Fixed #143 Update RSSI and battery indicators on UI thread"