/***
 Copyright (c) 2015 CommonsWare, LLC

 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.commonsware.cwac.cam2.support;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.commonsware.cwac.cam2.ImageContext;

public class ConfirmationFragment extends Fragment {
  public interface Contract {
    void completeRequest(ImageContext imageContext, boolean isOK);
    void retakePicture();
  }

  private ImageView iv;
  private ImageContext imageContext;

  public static ConfirmationFragment newInstance() {
    ConfirmationFragment result=new ConfirmationFragment();
    Bundle args=new Bundle();

    result.setArguments(args);

    return(result);
  }

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

    setRetainInstance(true);
    setHasOptionsMenu(true);
  }

  @Override
  public void onAttach(Activity activity) {
    if (!(activity instanceof Contract)) {
      throw new IllegalStateException("Hosting activity must implement Contract interface");
    }

    super.onAttach(activity);
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    iv=new ImageView(getActivity());

    if (imageContext!=null) {
      loadImage();
    }

    return(iv);
  }

  @Override
  public void onHiddenChanged(boolean isHidden) {
    super.onHiddenChanged(isHidden);

    if (!isHidden) {
      ActionBar ab=getActivity().getActionBar();

      ab.setBackgroundDrawable(getActivity()
          .getResources()
          .getDrawable(com.commonsware.cwac.cam2.R.drawable.cwac_cam2_action_bar_bg_translucent));
      ab.setTitle("");

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setHomeAsUpIndicator(com.commonsware.cwac.cam2.R.drawable.cwac_cam2_ic_close_white);
      } else {
        ab.setIcon(com.commonsware.cwac.cam2.R.drawable.cwac_cam2_ic_close_white);
        ab.setDisplayShowHomeEnabled(true);
        ab.setHomeButtonEnabled(true);
      }
    }
  }

  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(com.commonsware.cwac.cam2.R.menu.cwac_cam2_confirm, menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId()==android.R.id.home) {
      getContract().completeRequest(imageContext, true);
    }
    else if (item.getItemId()== com.commonsware.cwac.cam2.R.id.cwac_cam2_ok) {
      getContract().completeRequest(imageContext, true);
    }
    else if (item.getItemId()== com.commonsware.cwac.cam2.R.id.cwac_cam2_retry) {
      getContract().retakePicture();
    }
    else {
      return(super.onOptionsItemSelected(item));
    }

    return(true);
  }

  public void setImage(ImageContext imageContext) {
    this.imageContext=imageContext;

    if (iv!=null) {
      loadImage();
    }
  }

  private Contract getContract() {
    return((Contract)getActivity());
  }

  private void loadImage() {
    iv.setImageBitmap(imageContext.buildPreviewThumbnail());
  }
}

@@ -2418,34 +2418,8 @@
 ble(
-com.commonsware.cwac.cam2.
 R.dr
@@ -2635,34 +2635,8 @@
 tor(
-com.commonsware.cwac.cam2.
 R.dr
@@ -2707,34 +2707,8 @@
 con(
-com.commonsware.cwac.cam2.
 R.dr
@@ -2950,34 +2950,8 @@
 ate(
-com.commonsware.cwac.cam2.
 R.me
@@ -3198,34 +3198,8 @@
 )== 
-com.commonsware.cwac.cam2.
 R.id
@@ -3314,34 +3314,8 @@
 )== 
-com.commonsware.cwac.cam2.
 R.id

fix erroneous package referece