package epson.print.imgsel;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.epson.cameracopy.p006ui.ImagePreviewActivity;

import epson.common.ScalableImageView;
import epson.print.R;

public class ImageViewMultiSelectFragment extends ImageViewBaseFragment {
    protected MyToggleButton mMyToggleButton;

    public static ImageViewMultiSelectFragment newInstance(String str) {
        ImageViewMultiSelectFragment imageViewMultiSelectFragment = new ImageViewMultiSelectFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ImagePreviewActivity.PARAM_PICTURE_FILENAME, str);
        imageViewMultiSelectFragment.setArguments(bundle);
        return imageViewMultiSelectFragment;
    }

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.fragment_image_view_multi, viewGroup, false);
        mImageView = (ScalableImageView) inflate.findViewById(R.id.imageView);
        mProgressBar = (ProgressBar) inflate.findViewById(R.id.imageLoadProgressBar);
        mMyToggleButton = new MyToggleButton(this, inflate.findViewById(R.id.myToggle), mListener.getSelector().isSelected(mFileName));
        super.onCreateView(layoutInflater, viewGroup, bundle);
        return inflate;
    }

    private void selectImageOnMultiFileMode(boolean z) {
        ImageSelector selector = mListener.getSelector();
        if (!z) {
            selector.remove(mFileName);
        } else if (!selector.canAdd()) {
            MyToggleButton myToggleButton = mMyToggleButton;
            if (myToggleButton != null) {
                myToggleButton.forceSetChecked(false);
            }
            try {
                LocalAlertDialogFragment.newInstance(getString(R.string.max_image), 2).show(getChildFragmentManager(), "alert dialog");
                return;
            } catch (IllegalStateException unused) {
                return;
            }
        } else {
            selector.add(mFileName);
        }
        getActivity().invalidateOptionsMenu();
    }

    static class MyToggleButton {
        private ImageViewMultiSelectFragment mImageViewMultiSelectFragment;
        private boolean mMyToggleChecked;
        private ImageView mToggleImage;
        private TextView mToggleText;

        public MyToggleButton(ImageViewMultiSelectFragment imageViewMultiSelectFragment, View view, boolean z) {
            mImageViewMultiSelectFragment = imageViewMultiSelectFragment;
            init(view, z);
        }

        public void init(View view, boolean z) {
            mToggleImage = (ImageView) view.findViewById(R.id.myToggleImage);
            mToggleImage.setClickable(false);
            mToggleText = (TextView) view.findViewById(R.id.myToggleText);
            mToggleText.setClickable(false);
            view.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    toggleCheckStatus();
                }
            });
            forceSetChecked(z);
        }

        private void toggleCheckStatus() {
            setMyChecked(!mToggleImage.isSelected());
        }

        public void forceSetChecked(boolean z) {
            mMyToggleChecked = z;
            syncTextState();
            mToggleImage.setSelected(mMyToggleChecked);
        }

        private void setMyChecked(boolean z) {
            if (z != mMyToggleChecked) {
                forceSetChecked(z);
                execCallback();
            }
        }

        private void execCallback() {
            ImageViewMultiSelectFragment imageViewMultiSelectFragment = mImageViewMultiSelectFragment;
            if (imageViewMultiSelectFragment != null) {
                imageViewMultiSelectFragment.selectImageOnMultiFileMode(mMyToggleChecked);
            }
        }

        private void syncTextState() {
            if (mMyToggleChecked) {
                mToggleText.setText(R.string.imgsel_view_pager_toggle_on);
            } else {
                mToggleText.setText(R.string.imgsel_view_pager_toggle_off);
            }
        }
    }
}
