"use strict";
appCore.controller("WatchlistHeroesCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "$uibModal", "$document",
    function ($state, $scope, $http, $timeout, vServices, $uibModal, $document) {
        $scope.watchlistsDatatable = [];
        $scope.myId=$scope.$root.gUser.id;
        $scope.$root.page.title = "Watchlist";
        $scope.watchlists = [];
        $scope.hero_names = [];
        $scope.tag_names = [];
        $scope.taggedHeroes = [];
       
        $scope.filter = {
            wlist: [],
            emp: [],
            selwatchlist: 'All',
            selemp: 'All',
        };
        const convertDataForAccount = res => {
            if (res) {
                $scope.filter.wlist = [];
                $scope.filter.wlist.push({wtl_id: 0, tag: "All"});
                res.tag_names.map((o) => $scope.filter.wlist.push({wtl_id: o.wtl_id, tag: o.tag}));
				
				$scope.filter.emp = [];
                $scope.filter.emp.push({hero_id: 0, emp_first_name: "All"});
                let herores = res.hero_names.filter((v, i) => res.hero_names.indexOf(v) === i)
				console.log(herores);
				res.hero_names.map((o) => $scope.filter.emp.push({hero_id: o.hero_id, emp_first_name: o.emp_first_name, emp_last_name: o.emp_last_name})); 
            }
        };
        const getWatchlists = (wlist, emp) => vServices.callApi("watchlist/watchlist_master_search?wtl_id=" + wlist + "&hero_id=" + emp, "GET", {}).then(function (res) {
            $scope.watchlists = res;
            $scope.watchlists.forEach(j=>{
                j.currentDate = moment().format('DD-MM-YYYY');
                j.expireEnd = moment(j.comment_datetime).add(j.tag_days,'days').format('DD-MM-YYYY');
                j.dateOne = new Date(j.currentDate);
                j.dateTwo = new Date(j.expireEnd);
                if(j.dateOne < j.dateTwo){
                    j.expire = 1;
                }
                else{
                    j.expire = 0;
                }
                });
                $scope.watchlistsDatatable.map((o) => {o.destroy();});
                $scope.$root.callDataTable("#watchList", 500,(t) => {
                $scope.watchlistsDatatable.push(t);
                }, {orientation: "landscape"});
            
            // if (res && res.length > 0) {
            //     res.map((o) => {
            //         // o.createdAt = o.createdAt ? moment(o.createdAt) : "";
            //         //o.proj_expected_end_date = o.proj_expected_end_date ? moment(o.proj_expected_end_date) : "";
            //        // o.lastAllocationVerified = o.lastAllocationVerified ? moment(o.lastAllocationVerified) : "No Previous Allocation";
            //     });
            //     $scope.watchlistsDatatable.map((o) => {o.destroy();});
            //     $scope.watchlists = res;
            //     $scope.watchlists.forEach(j=>{
            //         j.currentDate = moment().format('DD-MM-YY');
            //         j.expireEnd = moment(j.comment_datetime).add(j.tag_days,'days').format('DD-MM-YY');
            //         if(j.currentDate >= j.expireEnd){
            //             j.expire = 1;
            //         }
            //         else{
            //             j.expire = 0;
            //         }
            //         });
            //     $scope.$root.callDataTable("#watchList", 500,(t) => {
            //         $scope.watchlistsDatatable.push(t);
            //     }, {orientation: "landscape"});
            // }
            // else{
            //     toastr.error("Sorry, Did not find any data for your Query !");
            // }
        });
        $scope.selectChanged = () => getWatchlists($scope.filter.selwatchlist, $scope.filter.selemp);
		
        const init = function () {
            // $scope.selectChanged();
            vServices.callApi("watchlist/watchlistTag_all", "GET", {}).then(res => {
				$scope.hero_names = res.hero_names;
				$scope.tag_names = res.tag_names;
			});

            vServices.callApi("watchlist/taggedHeroes", "GET", {}).then(res => {
				$scope.taggedHeroes = res;
               
			});

            vServices.callApi("watchlist/watchlist_master_search?wtl_id=" + 0 + "&hero_id=" + 0, "GET", {}).then(function (res) {
                $scope.watchlists = res.filter(function (el) {
                    return el != null;
                  });
                console.log($scope.watchlists);
                $scope.watchlists.forEach(j=>{
                    j.currentDate = moment().format('DD-MM-YYYY');
                    j.expireEnd = moment(j.comment_datetime).add(j.tag_days,'days').format('DD-MM-YYYY');
                    // if(j.currentDate > j.expireEnd){
                    j.dateOne = new Date(j.currentDate);
                    j.dateTwo = new Date(j.expireEnd);
                    if(j.dateOne < j.dateTwo){
                        j.expire = 0;
                    }
                    else{
                        j.expire = 1;
                    }
                });
                $scope.watchlistsDatatable.map((o) => {o.destroy();});
                $scope.$root.callDataTable("#watchList", 500,(t) => {
                $scope.watchlistsDatatable.push(t);
                }, {orientation: "landscape"});
            });
            
            
        };
        init();
        
        $scope.goToFeedback = (d) => {
            localStorage.setItem("watchlist_tag_id",d.wtl_id);
            window.open(
                '/heroes/feedback/'+d.hero_id,
                '_blank' // <- This is what makes it open in a new window.
              );
            // window.location.href = '/heroes/feedback/'+d.hero_id;
        }

        $scope.allTags = (id) => {
            vServices.callApi("watchlist/watchlist_master_search?wtl_id=" + 0 + "&hero_id=" + 0, "GET", {}).then(function (res) {
                res = res.filter(function (el) {
                    return el != null;
                  });
                if(id != '' ){
                    res = res.filter(j => j.wtl_id == id);
                    $scope.watchlists = res;
                }else{
                    $scope.watchlists = res;
                }
                $scope.watchlists.forEach(j=>{
                    j.currentDate = moment().format('DD-MM-YYYY');
                    j.expireEnd = moment(j.comment_datetime).add(j.tag_days,'days').format('DD-MM-YYYY');
                    j.dateOne = new Date(j.currentDate);
                    j.dateTwo = new Date(j.expireEnd);
                    if(j.dateOne < j.dateTwo){
                        j.expire = 0;
                    }
                    else{
                        j.expire = 1;
                    }
                });
                $scope.watchlistsDatatable.map((o) => {o.destroy();});
                $scope.$root.callDataTable("#watchList", 500,(t) => {
                $scope.watchlistsDatatable.push(t);
                }, {orientation: "landscape"});
            });
        }

        $scope.heroAndtags = (tagId,heroId) =>{
			$scope.tagId=(tagId==undefined || tagId==''  || tagId==null)?0:tagId;
			$scope.heroId=(heroId==undefined || heroId==''  || heroId==null)?0:heroId;
			tagId=$scope.tagId;
            heroId = $scope.heroId;
            if((tagId != '' || tagId!=0)  && heroId != 0){
                vServices.callApi("watchlist/watchlist_master_search?wtl_id=" + 0 + "&hero_id=" + 0, "GET", {}).then(function (res) {
                    res = res.filter(function (el) {
                        return el != null;
                      });
                    res = res.filter(j => j.wtl_id == tagId && j.hero_id == heroId);
                    $scope.watchlists = res;
                    $scope.watchlists.forEach(j=>{
                        j.currentDate = moment().format('DD-MM-YYYY');
                        j.expireEnd = moment(j.comment_datetime).add(j.tag_days,'days').format('DD-MM-YYYY');
                        j.dateOne = new Date(j.currentDate);
                        j.dateTwo = new Date(j.expireEnd);
                        if(j.dateOne < j.dateTwo){
                            j.expire = 0;
                        }
                        else{
                            j.expire = 1;
                        }
                    });
                    $scope.watchlistsDatatable.map((o) => {o.destroy();});
                    $scope.$root.callDataTable("#watchList", 500,(t) => {
                    $scope.watchlistsDatatable.push(t);
                    }, {orientation: "landscape"});
                });


            }
			else if((tagId == '' || tagId==0) && heroId != 0 ){
				 vServices.callApi("watchlist/watchlist_master_search?wtl_id=" + 0 + "&hero_id=" + 0, "GET", {}).then(function (res) {
                    res = res.filter(j => j.hero_id == heroId);
                    $scope.watchlists = res;
                    $scope.watchlists.forEach(j=>{
                        j.currentDate = moment().format('DD-MM-YYYY');
                        j.expireEnd = moment(j.comment_datetime).add(j.tag_days,'days').format('DD-MM-YYYY');
                        j.dateOne = new Date(j.currentDate);
                        j.dateTwo = new Date(j.expireEnd);
                        if(j.dateOne < j.dateTwo){
                            j.expire = 0;
                        }
                        else{
                            j.expire = 1;
                        }
                    });
                    $scope.watchlistsDatatable.map((o) => {o.destroy();});
                    $scope.$root.callDataTable("#watchList", 500,(t) => {
                    $scope.watchlistsDatatable.push(t);
                    }, {orientation: "landscape"});
                });
			}
			else if((tagId == '' || tagId==0) && heroId == 0 ){
				 vServices.callApi("watchlist/watchlist_master_search?wtl_id=" + 0 + "&hero_id=" + 0, "GET", {}).then(function (res) {
                   // res = res.filter(j => j.hero_id == heroId);
                    $scope.watchlists = res.filter(function (el) {
                        return el != null;
                      });;
                    $scope.watchlists.forEach(j=>{
                        j.currentDate = moment().format('DD-MM-YYYY');
                        j.expireEnd = moment(j.comment_datetime).add(j.tag_days,'days').format('DD-MM-YYYY');
                        j.dateOne = new Date(j.currentDate);
                        j.dateTwo = new Date(j.expireEnd);
                        if(j.dateOne < j.dateTwo){
                            j.expire = 0;
                        }
                        else{
                            j.expire = 1;
                        }
                    });
                    $scope.watchlistsDatatable.map((o) => {o.destroy();});
                    $scope.$root.callDataTable("#watchList", 500,(t) => {
                    $scope.watchlistsDatatable.push(t);
                    }, {orientation: "landscape"});
                });
			}
			else{
                vServices.callApi("watchlist/watchlist_master_search?wtl_id=" + 0 + "&hero_id=" + 0, "GET", {}).then(function (res) {
                    res = res.filter(function (el) {
                        return el != null;
                      });
                    res = res.filter(j => j.wtl_id == tagId);
                    $scope.watchlists = res;
                    $scope.watchlists.forEach(j=>{
                        j.currentDate = moment().format('DD-MM-YYYY');
                        j.expireEnd = moment(j.comment_datetime).add(j.tag_days,'days').format('DD-MM-YYYY');
                        j.dateOne = new Date(j.currentDate);
                        j.dateTwo = new Date(j.expireEnd);
                        if(j.dateOne < j.dateTwo){
                            j.expire = 0;
                        }
                        else{
                            j.expire = 1;
                        }
                    });
                    $scope.watchlistsDatatable.map((o) => {o.destroy();});
                    $scope.$root.callDataTable("#watchList", 500,(t) => {
                    $scope.watchlistsDatatable.push(t);
                    }, {orientation: "landscape"});
                });
            }

        }

    }
]);
