package li.doerf.hacked.ui.fragments;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import li.doerf.hacked.HackedApplication;
import li.doerf.hacked.R;
import li.doerf.hacked.db.AppDatabase;
import li.doerf.hacked.db.daos.AccountDao;
import li.doerf.hacked.db.entities.Account;
import li.doerf.hacked.ui.HibpInfo;
import li.doerf.hacked.ui.adapters.BreachesAdapter;
import li.doerf.hacked.ui.viewmodels.BreachViewModel;
import li.doerf.hacked.utils.BackgroundTaskHelper;

/**
 * Created by moo on 06/10/16.
 */
public class BreachDetailsFragment extends Fragment {
    private final String LOGTAG = getClass().getSimpleName();
//    private Account myAccount;
    private BreachesAdapter myBreachesAdapter;
    private long myAccountId;
    private BreachViewModel myViewModel;

    public static BreachDetailsFragment create(long accountId) {
        BreachDetailsFragment fragment = new BreachDetailsFragment();
        fragment.myAccountId = accountId;
        return fragment;
    }


    @SuppressLint("CheckResult")
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        AccountDao accountDao = AppDatabase.get(context).getAccountDao();
        myViewModel = ViewModelProviders.of(this).get(BreachViewModel.class);
        myBreachesAdapter = new BreachesAdapter(getContext(), new ArrayList<>());

        new BackgroundTaskHelper<Account>().runInBackgroundAndConsumeOnMain(() -> accountDao.findById(myAccountId), account -> {
            getActivity().setTitle(account.getName());
            myViewModel.getBreachList(
                    account.getId()).observe(
                    BreachDetailsFragment.this, accounts -> myBreachesAdapter.addItems(accounts));
        });
    }

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

        // TODO do something about this
//        if ( myBreachesAdapter.getItemCount() == 0 ) {
//            CardView noBreachFound = (CardView) view.findViewById(R.id.no_breach_found);
//            noBreachFound.setVisibility(View.VISIBLE);
//            TextView hibpInfo = (TextView) view.findViewById(R.id.hibp_info);
//            hibpInfo.setVisibility(View.GONE);
//            return view;
//        }

        RecyclerView breachesList = (RecyclerView) view.findViewById(R.id.breaches_list);
        breachesList.setHasFixedSize(true);
        LinearLayoutManager lm = new LinearLayoutManager(getContext());
        breachesList.setLayoutManager(lm);
        breachesList.setAdapter(myBreachesAdapter);

        HibpInfo.prepare( getContext(), (TextView) view.findViewById(R.id.hibp_info), breachesList);

        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(breachesList.getContext(),
                lm.getOrientation());
        breachesList.addItemDecoration(dividerItemDecoration);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        ((HackedApplication) getActivity().getApplication()).trackView("Fragment~BreachDetails");
    }

}

@@ -226,40 +226,8 @@
 oup;
-%0Aimport android.widget.TextView;
 %0A%0Aim
@@ -286,24 +286,66 @@
 n.Nullable;%0A
+import androidx.cardview.widget.CardView;%0A
 import andro
@@ -1064,16 +1064,18 @@
 gment %7B%0A
+//
     priv
@@ -1132,41 +1132,8 @@
 ();%0A
-//    private Account myAccount;%0A
     
@@ -1456,17 +1456,16 @@
 %0A    %7D%0A%0A
-%0A
     @Sup
@@ -1584,82 +1584,8 @@
 t);%0A
-        AccountDao accountDao = AppDatabase.get(context).getAccountDao();%0A
     
@@ -1740,16 +1740,390 @@
 t%3C%3E());%0A
+    %7D%0A%0A    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) %7B%0A        View view = inflater.inflate(R.layout.fragment_breach_details, container, false);%0A        CardView noBreachFound = view.findViewById(R.id.no_breach_found);%0A%0A        AccountDao accountDao = AppDatabase.get(getContext()).getAccountDao();
 %0A       
@@ -2448,46 +2448,9 @@
  -%3E 
-myBreachesAdapter.addItems(accounts));
+%7B
 %0A   
@@ -2458,242 +2458,70 @@
     
-%7D);%0A
     
-%7D%0A%0A
     
-public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) %7B%0A        View view = inflater.inflate(R.layout.fragment_breach_details, 
+        myBreachesAdapter.addItems(ac
 co
+u
 nt
-ainer, false
+s
 );%0A
-%0A
         
 // T
@@ -2520,42 +2520,16 @@
     
-// TODO do something about this%0A//
+        
     
@@ -2579,18 +2579,16 @@
 = 0 ) %7B%0A
-//
         
@@ -2595,87 +2595,12 @@
     
-CardView noBreachFound = (CardView) view.findViewById(R.id.no_breach_found);%0A//
+    
     
@@ -2646,26 +2646,24 @@
 w.VISIBLE);%0A
-//
             
@@ -2666,96 +2666,70 @@
     
-TextView hibpInfo = (TextView) view.findViewById(R.id.hibp_info);%0A//            hibpInfo
+            %7D else %7B%0A                            noBreachFound
 .set
@@ -2751,18 +2751,16 @@
 .GONE);%0A
-//
         
@@ -2767,23 +2767,46 @@
     
-return view;%0A//
+            %7D%0A                    %7D);%0A
     
@@ -2806,24 +2806,26 @@
 );%0A        %7D
+);
 %0A%0A        Re
@@ -2853,23 +2853,8 @@
 st =
- (RecyclerView)
  vie
@@ -3104,111 +3104,8 @@
 r);%0A
-%0A        HibpInfo.prepare( getContext(), (TextView) view.findViewById(R.id.hibp_info), breachesList);%0A%0A
     
@@ -3305,24 +3305,115 @@
 coration);%0A%0A
+        HibpInfo.prepare( getContext(), view.findViewById(R.id.hibp_info), breachesList);%0A%0A
         retu

remove todo