package com.battlelancer.seriesguide.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.battlelancer.seriesguide.R;
import com.battlelancer.seriesguide.api.SeriesGuideExtension;
import com.battlelancer.seriesguide.extensions.ExtensionManager;
import com.battlelancer.seriesguide.extensions.ExtensionsConfigurationFragment;
import com.battlelancer.seriesguide.util.Utils;
import com.battlelancer.seriesguide.util.ViewTools;
import org.greenrobot.eventbus.EventBus;

/**
 * Creates views for a list of {@link com.battlelancer.seriesguide.extensions.ExtensionManager.Extension}.
 */
public class ExtensionsAdapter extends ArrayAdapter<ExtensionManager.Extension> {

    public class ExtensionDisableRequestEvent {
        public int position;

        public ExtensionDisableRequestEvent(int position) {
            this.position = position;
        }
    }

    private static final int LAYOUT_EXTENSION = R.layout.item_extension;
    private static final int LAYOUT_ADD = R.layout.item_extension_add;

    private static final int VIEW_TYPE_EXTENSION = 0;
    private static final int VIEW_TYPE_ADD = 1;

    private final LayoutInflater layoutInflater;

    public ExtensionsAdapter(Context context) {
        super(context, 0);
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // extra row for add button
        return super.getCount() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        // last row is an add button
        return position == getCount() - 1 ? VIEW_TYPE_ADD : VIEW_TYPE_EXTENSION;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @NonNull
    @Override
    public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
        if (getItemViewType(position) == VIEW_TYPE_ADD) {
            if (convertView == null) {
                convertView = layoutInflater.inflate(LAYOUT_ADD, parent, false);
            }
            // warn non-supporters that they only can add a few extensions
            boolean isAtLimit =
                    getCount() - 1 == ExtensionsConfigurationFragment.EXTENSION_LIMIT_FREE
                            && !Utils.hasAccessToX(getContext());
            TextView textViewAdd = convertView.findViewById(R.id.textViewItemExtensionAddLabel);
            ViewTools.setVectorIconLeft(getContext().getTheme(), textViewAdd,
                    R.drawable.ic_add_white_24dp);
            textViewAdd.setVisibility(isAtLimit ? View.GONE : View.VISIBLE);
            convertView.findViewById(R.id.textViewItemExtensionAddLimit)
                    .setVisibility(isAtLimit ? View.VISIBLE : View.GONE);
            return convertView;
        }

        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(LAYOUT_EXTENSION, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final ExtensionManager.Extension extension = getItem(position);

        viewHolder.description.setText(extension.description);

        // title
        viewHolder.title.setText(extension.label);

        // icon
        if (extension.icon != null) {
            viewHolder.icon.setImageDrawable(extension.icon);
        } else {
            viewHolder.icon.setImageResource(R.mipmap.ic_app);
        }

        // overflow menu
        viewHolder.settings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popupMenu = new PopupMenu(v.getContext(), v);
                popupMenu.getMenuInflater().inflate(R.menu.extension_menu, popupMenu.getMenu());
                if (extension.settingsActivity == null) {
                    MenuItem item = popupMenu.getMenu()
                            .findItem(R.id.menu_action_extension_settings);
                    item.setVisible(false);
                    item.setEnabled(false);
                }
                popupMenu.setOnMenuItemClickListener(new OverflowItemClickListener(position));
                popupMenu.show();
            }
        });

        return convertView;
    }

    private class OverflowItemClickListener implements PopupMenu.OnMenuItemClickListener {

        private final int position;

        public OverflowItemClickListener(int position) {
            this.position = position;
        }

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.menu_action_extension_settings:
                    ExtensionManager.Extension extension = getItem(position);
                    // launch settings activity
                    Utils.tryStartActivity(getContext(), new Intent()
                                    .setComponent(extension.settingsActivity)
                                    .putExtra(SeriesGuideExtension.EXTRA_FROM_SERIESGUIDE_SETTINGS,
                                            true),
                            true
                    );
                    ExtensionManager.get().clearActionsCache();
                    return true;
                case R.id.menu_action_extension_disable:
                    EventBus.getDefault()
                            .post(new ExtensionDisableRequestEvent(position));
                    return true;
            }
            return false;
        }
    }

    static class ViewHolder {
        @BindView(R.id.imageViewItemExtensionIcon) ImageView icon;
        @BindView(R.id.textViewItemExtensionTitle) TextView title;
        @BindView(R.id.textViewItemExtensionDescription) TextView description;
        @BindView(R.id.imageViewItemExtensionSettings) ImageView settings;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

@@ -1110,24 +1110,30 @@
      public 
+final 
 int position
@@ -1126,32 +1126,32 @@
 l int position;%0A
-
 %0A        public 
@@ -1648,17 +1648,16 @@
 later = 
-(
 LayoutIn
@@ -1666,66 +1666,21 @@
 ater
-) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE
+.from(context
 );%0A 
@@ -3537,24 +3537,99 @@
 m(position);
+%0A        if (extension == null) %7B%0A            return convertView;%0A        %7D
 %0A%0A        vi
@@ -5312,16 +5312,65 @@
 ctivity%0A
+                    if (extension != null) %7B%0A    
         
@@ -5463,24 +5463,28 @@
             
+   
+ 
 .setComponen
@@ -5513,16 +5513,20 @@
 tivity)%0A
+    
         
@@ -5657,24 +5657,28 @@
             
+ 
+   
 true),%0A     
@@ -5704,13 +5704,21 @@
     
+    
 true%0A
+    
     
@@ -5736,16 +5736,38 @@
      );%0A
+                    %7D%0A
         

use LayoutInflater.from, add null checks.