package com.breadwallet.presenter.fragments;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.breadwallet.R;
import com.breadwallet.BreadWalletApp;
import com.breadwallet.presenter.activities.MainActivity;
import com.breadwallet.presenter.entities.TransactionListItem;
import com.breadwallet.tools.adapter.TransactionListAdapter;
import com.breadwallet.tools.animation.BRAnimator;
import com.breadwallet.tools.util.BRConstants;
import com.breadwallet.tools.util.BRStringFormatter;
import com.breadwallet.tools.manager.CurrencyManager;
import com.breadwallet.tools.util.CustomLogger;
import com.breadwallet.tools.manager.SharedPreferencesManager;
import com.breadwallet.tools.util.Utils;
import com.breadwallet.tools.adapter.MiddleViewAdapter;
import com.breadwallet.wallet.BRPeerManager;
import com.breadwallet.wallet.BRWalletManager;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 * BreadWallet
 * <p/>
 * Created by Mihail Gutan <mihail@breadwallet.com> on 8/4/15.
 * Copyright (c) 2016 breadwallet LLC
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

public class FragmentSettingsAll extends Fragment {
    private static final String TAG = FragmentSettingsAll.class.getName();
    public static TransactionListItem[] transactionObjects;
    public static ListView transactionList;
    public static TransactionListAdapter adapter;
    private static boolean refreshTransactionAvailable = true;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // The last two arguments ensure LayoutParams are inflated
        // properly.
        final View rootView = inflater.inflate(R.layout.fragment_settings_all, container, false);

        transactionList = (ListView) rootView.findViewById(R.id.transactions_list);


        adapter = new TransactionListAdapter(getActivity(), transactionObjects);
        if (transactionObjects != null) {
            if (transactionObjects.length == 0) transactionObjects = null;
        }
        if (transactionList != null)
            transactionList.setAdapter(adapter);
        refreshTransactions(getActivity());
        return rootView;
    }

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

    @Override
    public void onResume() {
        super.onResume();
        MiddleViewAdapter.resetMiddleView(getActivity(), null);
        refreshUI();
        adapter.notifyDataSetChanged();
    }

    public static void refreshTransactions(final Activity ctx) {
        if (refreshTransactionAvailable) {
            refreshTransactionAvailable = false;
            Log.e(TAG, "refreshTransactions");
            new Thread(new Runnable() {
                @Override
                public void run() {
                    transactionObjects = BRWalletManager.getInstance(ctx).getTransactions();
                    if (ctx != null && ctx instanceof MainActivity) {
                        ctx.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                refreshTransactionAvailable = true;
                                refreshUI();
                            }
                        });
                    }
                }
            }).start();
        }

    }

    public static void refreshUI() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (BRAnimator.level != 1) return;
                if (adapter != null) {
                    adapter.updateData(transactionObjects);
                    adapter.notifyDataSetChanged();
                }
            }
        },1000);

    }

    public static RelativeLayout getSeparationLine(int MODE, Activity ctx) {
        //0 - regular , 1 - with left padding
        RelativeLayout line = new RelativeLayout(ctx);
        line.setMinimumHeight(1);
        line.setBackgroundColor(ctx.getColor(R.color.gray));
        if (MODE == 1)
            line.setPadding(40, 0, 0, 0);
        return line;
    }

}

@@ -116,194 +116,17 @@
 oid.
-content.Context;%0Aimport android.content.res.Resources;%0Aimport android.graphics.Color;%0Aimport android.os.Bundle;%0Aimport android.os.Handler;%0Aimport android.support.v4.content.ContextCompat
+os.Bundle
 ;%0Aim
@@ -245,74 +245,8 @@
 up;%0A
-import android.widget.Button;%0Aimport android.widget.LinearLayout;%0A
 impo
@@ -315,104 +315,33 @@
 ut;%0A
-import android.widget.TextView;%0A%0Aimport com.breadwallet.R;%0Aimport com.breadwallet.BreadWalletApp
+%0Aimport com.breadwallet.R
 ;%0Aim
@@ -575,314 +575,8 @@
 or;%0A
-import com.breadwallet.tools.util.BRConstants;%0Aimport com.breadwallet.tools.util.BRStringFormatter;%0Aimport com.breadwallet.tools.manager.CurrencyManager;%0Aimport com.breadwallet.tools.util.CustomLogger;%0Aimport com.breadwallet.tools.manager.SharedPreferencesManager;%0Aimport com.breadwallet.tools.util.Utils;%0A
 impo
@@ -631,53 +631,8 @@
 er;%0A
-import com.breadwallet.wallet.BRPeerManager;%0A
 impo
@@ -679,124 +679,8 @@
 r;%0A%0A
-import java.math.BigDecimal;%0Aimport java.text.SimpleDateFormat;%0Aimport java.util.Calendar;%0Aimport java.util.Locale;%0A
 %0A/**
@@ -4251,121 +4251,8 @@
 ) %7B%0A
-        new Handler().postDelayed(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A        
     
@@ -4290,24 +4290,16 @@
 return;%0A
-        
         
@@ -4329,32 +4329,24 @@
             
-        
 adapter.upda
@@ -4381,32 +4381,24 @@
             
-   
-     
 adapter.noti
@@ -4429,48 +4429,9 @@
     
-        %7D%0A            %7D%0A        %7D,1000);
+%7D
 %0A%0A  

remove delay UI update