<template>
  <b-jumbotron>
    <template slot="header">
      {{title}}
    </template>
    <hr class="my-4">
    <template slot="lead">
        Need to share someting ? Watch live, how your work is doing !
    </template>
    <template v-if="isLogged">
        Now you are logged as {{logon.user.displayname}}. Welcome. Whenever you can <b-btn variant="primary" @click="logout">Log out</b-btn>
    </template>
    <template v-else>
      Anyone can see whats whats up, but only priviledged can manage...
      <b-btn variant="success" @click="login">Log In to manage</b-btn>
    </template>
    <br>
    <template v-if="statusLoaded">
        Watcher service is currently
       <template v-if="isServiceRunning"> 
              <b-badge v-if="status.paused" pill variant="secondary">paused</b-badge>
              <b-badge v-if="status.paused!=true" pill variant="success">up</b-badge>
          , last scanning at: {{status.last_update}}.</template>
        <template v-else>
          <b-badge pill variant="danger">down</b-badge>.
    </template>

      <template v-if="isAdmin">
        <template v-if="isServiceRunning">
          <template v-if="status.paused">
            You can 
            <b-btn pill variant="outline-success" size="sm" @click="resumeService">resume service</b-btn>
            right now.
            </template>
          <template v-else>
            You can 
            <b-btn pill variant="secondary" size="sm" @click="pauseService">pause it</b-btn>
            right now.
          </template>  
        </template>    
        <template v-else>
          You should ask admin to start it. 
        </template>
      </template>
      View service 
      <b-btn pill variant="info" size="sm" @click="OpenLogs">LogFile</b-btn>.
    </template>

    <template v-else>
      Checking watcher service status...
    </template>                    
  </b-jumbotron>
</template>

<script>

module.exports = {
    data: function(){
          return {
              title: "RemoteTorrentSystem",
              logon:{
                user:{
                   displayname:null
                    }
                  },
              supressNTLM:false,
              statusLoaded:false,
              status:null,
              isAdmin:false
              }
        },
   
    created: function()
        {
          this.checkLogon();
          this.loadStatus();
          setInterval(this.loadStatus, 5000);
        },
    computed: {
          isLogged:function()
          {
            return this.logon.user.displayname !==null;
          },
          isServiceRunning:function()
          {
            try {
              return this.status.serviceRunning;
            }catch (e){
               return false;
            }           
          },
          msgToShow:function()
          { 
            return this.messages.length>0;
          }

        },
    methods:{
          OpenLogs:function()
          {
            window.open(this.status.logFile);
          },
          stopService:function()
          {
            this.updateStatus({serviceRunning:false});
          },
          startService:function()
          {
            this.updateStatus({serviceRunning:true});
          },
          resumeService:function()
          {   
            this.updateStatus({paused:false});
          },
          pauseService:function()
          {
            this.updateStatus({paused:true});
          },
          updateStatus:function(st)
          {
            var vm = this
            axios.post(apiPath+"watcher/status","status="+JSON.stringify(st))
                 .then(function (response) {
                   if(response.data.data)
                      vm.loadStatus();
                      vm.messages=response.data.messages;//console.log(response.data.messages.length);console.log(vm.messages);console.log(vm.messages.length);
                  })
                  .catch(function (error) {
                    vm.messages.push({type:"err",text:'Could not reach the API.' + error});
                    vm.statusLoaded=false;
                  })
          },
          loadStatus:function()
          {
            var vm = this
            axios.get(apiPath+"watcher/status")
                 .then(function (response) {
                      vm.status=response.data.data;
                      vm.statusLoaded=true;
                  })
                  .catch(function (error) {
                    errors.push('Could not reach the API. ' + error);
                    vm.statusLoaded=false;
                  })
          },
          login:function()
          {
            var vm = this
            axios.post(apiPath+"login",vm.supressNTLM)
                 .then(function (response) {
                   if(response.data.data !==null)
                   { 
                      vm.logon.user=response.data.data;
                      vm.checkAdmin();//console.log(vm.logon.user)
                    }
                  })
                  .catch(function (error) {
                    vm.messages.push('Could not reach the API. ' + error);
                  })
          },
          logout:function(){
            var vm = this
            axios.post(apiPath+"login/logout")
                 .then(function (response) {
                   if(response.data.data !==null)
                   {  vm.logon.user={displayname:null};
                      vm.supressNTLM="supressNTLM=true";
                      vm.checkAdmin();
                    }
                  })
                  .catch(function (error) {
                    vm.messages.push('Could not reach the API. ' + error);
                  })
          },
          checkLogon:function()
          {
            var vm = this
            axios.get(apiPath+"login")
                 .then(function (response) {
                   //console.log(response.data.data);
                   if(response.data.data !==null)
                   {              
                      vm.logon.user=response.data.data;
                      vm.logon.logged=true;
                      vm.checkAdmin();
                    }
                  })
                  .catch(function (error) {
                    vm.messages.push('Could not reach the API. ' + error);
                  })
          },
          checkAdmin:function()
          {
              try { 
                var res=this.logon.user !==null && this.logon.user.admin;
                this.isAdmin=res;
                this.$emit("adminchanged",res);
                return res;
            }catch (e){console.log(e);
              return false;
            }   
          }
    }
}

</script>