#!/usr/bin/perl
# Plugin for TWiki Collaboration Platform, http://TWiki.org/
#
# Copyright (C) 2008-2009 Peter Thoeny, peter@twiki.net
#               2008-2009 Sopan Shewale sopan.shewale@gmail.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details, published at
# http://www.gnu.org/copyleft/gpl.html
#

use strict;
use warnings;

# Set library paths in @INC, at compile time
BEGIN { unshift @INC, '.'; require 'setlib.cfg' }

use CGI;
use TWiki;
use TWiki::Func;
use TWiki::Plugins;
use TWiki::Plugins::NotificationPlugin;
use Data::Dumper;

my $query = new CGI();

my $twiki = new TWiki( undef, $query );
$TWiki::Plugins::SESSION = $twiki;
my $wikiName =
  TWiki::Func::getWikiName();    ## SopanShewale and not Main.SopanShewale

my $webName       = $twiki->{'webName'};
my $topic         = $twiki->{'topicName'};
my $scriptUrlPath = TWiki::Func::getScriptUrlPath();

if ( $wikiName ne "TWikiGuest" ) {
    if ( $query->param("action") ) {
        if ( $query->param("what") ) {
            modify_notification( $webName, $topic, $query->param("what"),
                $query->param("action") );
        }
        else {

            # loop thru all possible checkboxes
            for (qw(TIN WIN TN WN)) {
                modify_notification( $webName, $topic, $_, $query->param($_) );
            }
        }
    }

    # All work is done; redirect if needed
    unless ( $query->param("popup") ) {
        TWiki::Func::writeDebug("URL = $scriptUrlPath/view/$webName/$topic");
        TWiki::redirect( $query, $scriptUrlPath . "/view/$webName/$topic" );
    }
}

#  Fallthru: do something if no Javascript
draw_checkboxes( $scriptUrlPath, $topic, $webName );

sub modify_notification {
    my ( $webName, $topic, $what, $action ) = @_;
    $action ||= '';

    my %tmp = ( "TIN" => 0, "WIN" => 1, "TN" => 3, "WN" => 4 );

    my $str = "$webName.$topic";
    $str = "$webName" if ( $tmp{$what} == 1 || $tmp{$what} == 4 );
    &TWiki::Func::writeDebug("WHAT = $what");
    &TWiki::Func::writeDebug("STR = $str");
    my ( $meta, $text ) = ( "", "" );
    if ( $action eq "ON" ) {
        ( $meta, $text ) =
          TWiki::Plugins::NotificationPlugin::addItemToNotifyList( $wikiName,
            $str, $tmp{$what} );
    }
    else {
        ( $meta, $text ) =
          TWiki::Plugins::NotificationPlugin::removeItemFromNotifyList(
            $wikiName, $str, $tmp{$what} );
    }
    TWiki::Plugins::NotificationPlugin::saveUserNotifyList( $wikiName, $meta,
        $text );
}

sub draw_checkboxes {
    my ( $scriptUrlPath, $topic, $webName ) = @_;

    my $tinOn = TWiki::Plugins::NotificationPlugin::isItemInSection( $wikiName,
        "$webName.$topic", 0 );
    my $winOn = TWiki::Plugins::NotificationPlugin::isItemInSection( $wikiName,
        "$webName", 1 );
    my $tnOn = TWiki::Plugins::NotificationPlugin::isItemInSection( $wikiName,
        "$webName.$topic", 3 );
    my $wnOn = TWiki::Plugins::NotificationPlugin::isItemInSection( $wikiName,
        "$webName", 4 );
    my $action = $scriptUrlPath . "/changenotify/" . $webName . "/" . $topic;
    my $html =
qq!<form onSubmit="setTimeout('window.close()',2000)" method="post" action="$action">
    <input type="hidden" name="popup" value="1" />
    <input type="checkbox" name="TIN" value="ON">Immediate Notification of changes to <b>$topic</b><br>
    <input type="checkbox" name="WIN" value="ON">Immediate Notification of changes to <b>$webName</b><br>
    <input type="checkbox" name="TN" value="ON" >Normal Notification of changes to <b>$topic</b><br>
    <input type="checkbox" name="WN" value="ON" >Normal Notification of changes to <b>$webName</b><br>
    <input type="submit" value="Update" name="action"></form>!;
    $html =~ s/(name="TIN")/$1 checked="checked"/ if $tinOn;
    $html =~ s/(name="WIN")/$1 checked="checked"/ if $winOn;
    $html =~ s/(name="TN")/$1 checked="checked"/  if $tnOn;
    $html =~ s/(name="WN")/$1 checked="checked"/  if $wnOn;
    TWiki::Func::writeHeader($query);
    print "<B>$webName.$topic</B>$html\n";
}
