/*
 * Copyright (C) 2012 Brian Muramatsu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.btmura.android.reddit.app;

import android.app.Activity;
import android.app.DialogFragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.Loader;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Spinner;

import com.btmura.android.reddit.R;
import com.btmura.android.reddit.content.AccountLoader;
import com.btmura.android.reddit.content.AccountLoader.AccountResult;
import com.btmura.android.reddit.provider.SubredditProvider;
import com.btmura.android.reddit.text.InputFilters;
import com.btmura.android.reddit.widget.AccountNameAdapter;

public class AddSubredditFragment extends DialogFragment implements
        LoaderCallbacks<AccountResult>,
        OnCheckedChangeListener,
        OnClickListener {

    public static final String TAG = "AddSubredditFragment";

    private static final InputFilter[] INPUT_FILTERS = new InputFilter[] {
            InputFilters.SUBREDDIT_NAME_FILTER,
    };

    private SubredditNameHolder subredditNameHolder;
    private AccountNameAdapter adapter;
    private boolean restoringState;
    private Spinner accountSpinner;
    private EditText nameField;
    private CheckBox addFrontPage;
    private Button cancel;
    private Button ok;

    public static AddSubredditFragment newInstance() {
        return new AddSubredditFragment();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof SubredditNameHolder) {
            subredditNameHolder = (SubredditNameHolder) activity;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new AccountNameAdapter(getActivity());
        restoringState = savedInstanceState != null;
    }

    @Override
    public View
            onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().setTitle(R.string.add_subreddit);
        View v = inflater.inflate(R.layout.add_subreddit, container, false);

        accountSpinner = (Spinner) v.findViewById(R.id.account_spinner);
        accountSpinner.setAdapter(adapter);

        CharSequence name = subredditNameHolder.getSubredditName();
        int length = name != null ? name.length() : 0;
        nameField = (EditText) v.findViewById(R.id.subreddit_name);
        nameField.setText(name);
        nameField.setSelection(length, length);
        nameField.setFilters(INPUT_FILTERS);

        addFrontPage = (CheckBox) v.findViewById(R.id.add_front_page);
        addFrontPage.setOnCheckedChangeListener(this);

        cancel = (Button) v.findViewById(R.id.cancel);
        cancel.setOnClickListener(this);

        ok = (Button) v.findViewById(R.id.ok);
        ok.setOnClickListener(this);

        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getLoaderManager().initLoader(0, null, this);
    }

    public Loader<AccountResult> onCreateLoader(int id, Bundle args) {
        return new AccountLoader(getActivity(), true);
    }

    public void onLoadFinished(Loader<AccountResult> loader, AccountResult result) {
        int visiblility = result.accountNames.length > 1 ? View.VISIBLE : View.GONE;
        accountSpinner.setVisibility(visiblility);
        adapter.setAccountNames(result.accountNames);
        if (!restoringState) {
            accountSpinner.setSelection(adapter.findAccountName(result.getLastAccount()));
        }
    }

    public void onLoaderReset(Loader<AccountResult> loader) {
        adapter.setAccountNames(null);
        dismiss();
    }

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        nameField.setEnabled(!isChecked);
        if (!nameField.isEnabled()) {
            nameField.setError(null);
        }
    }

    public void onClick(View v) {
        if (v == cancel) {
            handleCancel();
        } else if (v == ok) {
            handleOk();
        }
    }

    private void handleCancel() {
        dismiss();
    }

    private void handleOk() {
        String subredditName;
        if (addFrontPage.isChecked()) {
            subredditName = "";
        } else {
            subredditName = nameField.getText().toString();
        }

        if (!addFrontPage.isChecked() && TextUtils.isEmpty(subredditName)) {
            nameField.setError(getString(R.string.error_blank_field));
            return;
        }

        String accountName = adapter.getItem(accountSpinner.getSelectedItemPosition());
        SubredditProvider.insertInBackground(getActivity(), accountName, subredditName);
        dismiss();
    }
}

@@ -1637,24 +1637,16 @@
 plements
-%0A       
  LoaderC
@@ -1702,24 +1702,16 @@
 istener,
-%0A       
  OnClick
@@ -2793,28 +2793,16 @@
 lic View
-%0A           
  onCreat
@@ -2852,16 +2852,28 @@
 ntainer,
+%0A           
  Bundle 

Fix some formatting in AddSubredditFragment