package net.af0.sesame;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

/**
 * A fragment representing a single Item detail screen.
 * This fragment is either contained in a {@link ItemListActivity}
 * in two-pane mode (on tablets) or a {@link ItemDetailActivity}
 * on handsets.
 */
public class ItemDetailFragment extends Fragment {
    // The item we're showing details for.
    private SQLCipherDatabase.Record item_;

    private View rootView_;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public ItemDetailFragment() {
    }

    void loadFromDatabase() {
        item_ = SQLCipherDatabase.getRecord(getArguments().getLong(Constants.ARG_ITEM_ID));
        if (item_.getUsername() != null && !item_.getUsername().isEmpty()) {
            ((TextView) rootView_.findViewById(R.id.username)).setText(item_.getUsername());
        } else {
            rootView_.findViewById(R.id.username).setVisibility(View.INVISIBLE);
        }
        if (item_.getPassword() != null && !item_.getPassword().isEmpty()) {
            ((TextView) rootView_.findViewById(R.id.password)).setText(item_.getPassword());
        } else {
            rootView_.findViewById(R.id.password).setVisibility(View.INVISIBLE);
        }
        if (item_.getDomain() != null && !item_.getDomain().isEmpty()) {
            ((TextView) rootView_.findViewById(R.id.domain)).setText(item_.getDomain());
        } else {
            rootView_.findViewById(R.id.domain).setVisibility(View.INVISIBLE);
        }
        if (item_.getRemarks() != null && !item_.getRemarks().isEmpty()) {
            ((TextView) rootView_.findViewById(R.id.remarks)).setText(item_.getRemarks());
        } else {
            rootView_.findViewById(R.id.remarks).setVisibility(View.INVISIBLE);
        }
    }

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

        loadFromDatabase();

        // Make the show/hide switch change the password field visibility.
        ((Switch) rootView_.findViewById(R.id.show_password)).setOnCheckedChangeListener(
                new
                        CompoundButton.OnCheckedChangeListener() {
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                ((TextView) rootView_.findViewById(R.id.password)).setInputType(
                                        isChecked ?
                                                InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD :
                                                InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
                                );
                            }
                        }
        );

        return rootView_;
    }
}

@@ -1015,235 +1015,560 @@
     
-if (item_.getUsername() != null && !item_.getUsername().isEmpty()) %7B%0A            ((TextView) rootView_.findViewById(R.id.username)).setText(item_.getUsername());%0A        %7D else %7B%0A            rootView_.findViewById(R.id.
+TextView usernameView_ = ((TextView) rootView_.findViewById(R.id.username));%0A        TextView passwordView_ = ((TextView) rootView_.findViewById(R.id.password));%0A        TextView domainView_ = ((TextView) rootView_.findViewById(R.id.domain));%0A        TextView remarksView_ = ((TextView) rootView_.findViewById(R.id.remarks));%0A%0A        if (item_.getUsername() != null && !item_.getUsername().isEmpty()) %7B%0A            usernameView_.setText(item_.getUsername());%0A            usernameView_.setVisibility(View.VISIBLE);%0A        %7D else %7B%0A            
 username
 ).se
@@ -1563,17 +1563,21 @@
 username
-)
+View_
 .setVisi
@@ -1703,56 +1703,16 @@
     
-((TextView) rootView_.findViewById(R.id.
 password
 )).s
@@ -1703,26 +1703,29 @@
     password
-))
+View_
 .setText(ite
@@ -1755,65 +1755,92 @@
     
-%7D else %7B%0A            rootView_.findViewById(R.id.
+    passwordView_.setVisibility(View.VISIBLE);%0A        %7D else %7B%0A            
 password
 ).se
@@ -1835,17 +1835,21 @@
 password
-)
+View_
 .setVisi
@@ -1971,56 +1971,19 @@
     
-((TextView) rootView_.f
+doma
 in
-d
 View
-ById(R.id.domain))
+_
 .set
@@ -2003,24 +2003,77 @@
 tDomain());%0A
+            domainView_.setVisibility(View.VISIBLE);%0A
         %7D el
@@ -2093,43 +2093,19 @@
     
-rootView_.f
+doma
 in
-d
 View
-ById(R.id.domain)
+_
 .set
@@ -2233,152 +2233,145 @@
     
-((TextView) rootView_.findViewById(R.id.remarks)).setText(item_.getRemarks());%0A        %7D else %7B%0A            rootView_.findViewById(R.id.
+remarksView_.setText(item_.getRemarks());%0A            remarksView_.setVisibility(View.VISIBLE);%0A        %7D else %7B%0A            
 remarks
-)
+View_
 .set

Update field visibility on database reload.