/*
 * Copyright 2016 Peter Kenji Yamanaka
 *
 * 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.pyamsoft.powermanager.trigger;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import com.pyamsoft.powermanager.PowerManager;
import com.pyamsoft.powermanager.model.sql.PowerTriggerEntry;
import timber.log.Timber;

public class DeleteTriggerDialog extends DialogFragment {

  @NonNull private static final String TRIGGER_NAME = "trigger_name";
  @NonNull private static final String TRIGGER_PERCENT = "trigger_percent";
  @SuppressWarnings("WeakerAccess") int percent;
  private String name;

  @CheckResult @NonNull
  public static DeleteTriggerDialog newInstance(@NonNull PowerTriggerEntry trigger) {
    Bundle args = new Bundle();
    DeleteTriggerDialog fragment = new DeleteTriggerDialog();
    args.putString(TRIGGER_NAME, trigger.name());
    args.putInt(TRIGGER_PERCENT, trigger.percent());
    fragment.setArguments(args);
    return fragment;
  }

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    name = getArguments().getString(TRIGGER_NAME, null);
    percent = getArguments().getInt(TRIGGER_PERCENT, -1);

    if (percent == -1) {
      Timber.e("Invalid percent for DeleteTriggerDialog. Dismiss dialog");
      dismiss();
    }
  }

  @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new AlertDialog.Builder(getActivity()).setTitle("Delete Trigger")
        .setMessage("Really delete trigger for: " + name + " [" + percent + "%] ?")
        .setNegativeButton("Cancel", (dialogInterface, i) -> dismiss())
        .setPositiveButton("Okay", (dialogInterface, i) -> {
          sendDeleteEvent(percent);
          dismiss();
        })
        .create();
  }

  @SuppressWarnings("WeakerAccess") void sendDeleteEvent(int percent) {
    final FragmentManager fragmentManager = getFragmentManager();
    final Fragment powerTriggerListFragment =
        fragmentManager.findFragmentByTag(PowerTriggerListFragment.TAG);
    if (powerTriggerListFragment instanceof PowerTriggerListFragment) {
      ((PowerTriggerListFragment) powerTriggerListFragment).deleteTrigger(percent);
    } else {
      throw new ClassCastException("Fragment is not PowerTriggerListFragment");
    }
  }

  @Override public void onDestroy() {
    super.onDestroy();
    PowerManager.getRefWatcher(this).watch(this);
  }
}

@@ -2686,59 +2686,134 @@
     
-final FragmentManager fragmentManager = get
+Fragment powerTriggerFragment = getFragmentManager().findFragmentByTag(PowerTriggerFragment.TAG);%0A    if (powerTrigger
 Fragment
 Mana
@@ -2812,28 +2812,49 @@
 ment
-Manager();%0A    final
+ instanceof PowerTriggerFragment) %7B%0A     
  Fra
@@ -2889,18 +2889,39 @@
 nt =
-%0A        f
+ powerTriggerFragment.getChildF
 ragm
@@ -2930,16 +2930,29 @@
 tManager
+()%0A          
 .findFra
@@ -2985,32 +2985,34 @@
 tFragment.TAG);%0A
+  
     if (powerTri
@@ -3073,16 +3073,18 @@
 %7B%0A      
+  
 ((PowerT
@@ -3149,24 +3149,26 @@
 r(percent);%0A
+  
     %7D else %7B
@@ -3174,16 +3174,18 @@
 %7B%0A      
+  
 throw ne
@@ -3246,24 +3246,121 @@
 Fragment%22);%0A
+      %7D%0A    %7D else %7B%0A      throw new ClassCastException(%22Fragment is not PowerTriggerFragment%22);%0A
     %7D%0A  %7D%0A%0A 

Fix trigger delete