<template>
  <div
    class="relative items-start p-4 hover:bg-primary-light  hover:border-primary-light rounded-lg mb-2 shadow-lg border-2 cursor-pointer select-none"
    :class="computed_classes" 
    :title="title">
    <!-- CUSTOM MODEL VIEW -->
    <div class="flex flex-row" v-if="model.isCustomModel">
      <div class="max-w-[300px] overflow-x-auto">
        <div class="flex gap-3 items-center grow">
          <img :src="getImgUrl()" @error="defaultImg($event)" class="w-10 h-10 rounded-lg object-fill">
          <div class="flex-1 overflow-hidden">
            <h3 class="font-bold font-large text-lg truncate">
              {{ title }}
            </h3>
          </div>
        </div>
      </div>


    </div>

    <div v-if="model.isCustomModel" class="flex items-center flex-row gap-2 my-1">
      <!-- CONTROLS -->
      <div class="flex grow items-center">
        <button type="button" title="Custom model / local model"
          class="font-medium rounded-lg text-sm p-2 text-center inline-flex items-center " @click.stop="">
          <i data-feather="box" class="w-5"></i>
          <span class="sr-only">Custom model / local model</span>
        </button>
        Custom model
      </div>
      <button v-if="model.isInstalled" type="button" title="Select"
            @click="toggleSelected"
            class="hover:text-secondary duration-75 active:scale-90 font-medium rounded-lg text-sm p-2 text-center inline-flex items-center " @click.stop="">
            <i data-feather="check" class="w-5"></i>
            <span class="sr-only">Select</span>
      </button>        

      <div>
        <button v-if="model.isInstalled" title="Delete file from disk" type="button" @click.stop="toggleInstall"
          class="inline-flex items-center gap-2 px-3 py-2 text-xs font-medium text-center focus:outline-none text-white bg-red-700 hover:bg-red-800 focus:ring-4 focus:ring-red-300  rounded-lg  dark:bg-red-600 dark:hover:bg-red-700 dark:focus:ring-red-900">
          Uninstall
          <span class="sr-only">Remove</span>
        </button>
      </div>




    </div>
    <div v-if="installing"
      class="absolute z-10 -m-4 p-5 shadow-md text-center rounded-lg w-full h-full bg-bg-light-tone-panel dark:bg-bg-dark-tone-panel bg-opacity-70 dark:bg-opacity-70 flex justify-center items-center">
      <!-- DOWNLOAD MODEL PANEL SPINNER -->
      <div class="relative flex flex-col items-center justify-center flex-grow h-full">
        <div role="status" class=" justify-center ">
          <!-- SPINNER -->
          <svg aria-hidden="true" class="w-24 h-24 mr-2 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600"
            viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path
              d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
              fill="currentColor" />
            <path
              d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
              fill="currentFill" />
          </svg>
          <span class="sr-only">Loading...</span>
        </div>
        <div class="relative flex flex-row flex-grow items-center w-full h-full bottom-0">
          <!-- PROGRESS BAR -->
          <div class="w-full bg-bg-light-tone-panel dark:bg-bg-dark-tone-panel  rounded-lg p-2">


            <div class="flex justify-between mb-1">
              <span class="text-base font-medium text-blue-700 dark:text-white">Downloading</span>
              <span class="text-sm font-medium text-blue-700 dark:text-white">{{ Math.floor(progress) }}%</span>
            </div>
            <div class="w-full bg-gray-200 rounded-full h-2.5 dark:bg-gray-700">
              <div class="bg-blue-600 h-2.5 rounded-full" :style="{ width: progress + '%' }"></div>
            </div>
            <div class="flex justify-between mb-1">
              <span class="text-base font-medium text-blue-700 dark:text-white">Download speed: {{ speed_computed
              }}/s</span>
              <span class="text-sm font-medium text-blue-700 dark:text-white">{{ downloaded_size_computed }}/{{
                total_size_computed }}</span>
            </div>
          </div>
        </div>
        <div class="flex flex-grow">
          <!-- CANCEL BUTTON -->

          <div class="flex  flex-row flex-grow gap-3">
            <div class="p-2 text-center grow">
              <!-- <button @click.stop="hide(true)" type="button"
                                class="mr-2 text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm  sm:w-auto px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">
                                {{ ConfirmButtonText }}
                            </button> -->
              <button @click.stop="toggleCancelInstall" type="button" title="Cancel download"
                class="text-gray-500 bg-white hover:bg-gray-100 focus:ring-4 focus:outline-none focus:ring-gray-200 rounded-lg border border-gray-200 text-sm font-medium px-5 py-2.5 hover:text-gray-900 focus:z-10 dark:bg-gray-700 dark:text-gray-300 dark:border-gray-500 dark:hover:text-white dark:hover:bg-gray-600 dark:focus:ring-gray-600">
                Cancel
              </button>
            </div>

          </div>
        </div>
      </div>

    </div>
    <div v-if="!model.isCustomModel">
      <div class="flex flex-row items-center   gap-3 ">
        <img ref="imgElement" :src="getImgUrl()" @error="defaultImg($event)" class="w-10 h-10 rounded-lg object-fill"
          :class="linkNotValid ? 'grayscale' : ''">
        <h3 class="font-bold font-large text-lg truncate">
          {{ title }}
        </h3>
        <div class="grow">
          <!-- EMPTY SPACE FILLER -->
        </div>
        <button v-if="model.isInstalled" type="button" title="Select"
            @click="toggleSelected"
            class="hover:text-secondary duration-75 active:scale-90 font-medium rounded-lg text-sm p-2 text-center inline-flex items-center " @click.stop="">
            <i data-feather="check" class="w-5"></i>
            <span class="sr-only">Select</span>
        </button>        

        <InteractiveMenu  :commands="commandsList" :force_position=2 title="Menu">
        
        </InteractiveMenu>

      </div>
      <div class="flex items-center flex-row-reverse gap-2 my-1">
        <!-- CONTROLS -->

        <div class="flex flex-row  items-center ">

          <div v-if="linkNotValid" class="text-base text-red-600 flex  items-center mt-1 ">
            <i data-feather="alert-triangle" class="flex-shrink-0 mx-1"></i>
            Link is not valid
          </div>
        </div>


      </div>
      <div class="" :title="!model.isInstalled ? 'Not installed' : title">
        <div class="">
          <!-- <div class="flex flex-row  items-center ">

            <div v-if="linkNotValid" class="text-base text-red-600 flex  items-center mt-1 ">
              <i data-feather="alert-triangle" class="flex-shrink-0 mx-1"></i>
              Link is not valid
            </div>
          </div> -->
          <div class="flex flex-row  items-center ">

            <i data-feather="download" class="w-5 m-1 flex-shrink-0"></i>
            <b>Manual download:&nbsp;</b>



            <a :href="path" @click.stop
              class="m-1 flex items-center  hover:text-secondary duration-75 active:scale-90 truncate"
              :title="linkNotValid ? 'Link is not valid' : 'Download this manually (faster) and put it in the models/<current binding> folder then refresh'">


              Click here to download

            </a>
            <div class="grow"></div>
            <button
              class="hover:text-secondary duration-75 active:scale-90 font-medium rounded-lg text-sm p-2 text-center inline-flex items-center "
              title="Copy link to clipboard" @click.stop="toggleCopyLink()">
              <i data-feather="clipboard" class="w-5"></i>
            </button>

          </div>

          <div class="flex items-center">
            <div class="flex flex-shrink-0 items-center" :class="linkNotValid ? 'text-red-600' : ''">
              <i data-feather="file" class="w-5 m-1"></i>

              <b>File size:&nbsp;</b>

              {{ fileSize }}
            </div>

          </div>
          <div class="flex items-center">
            <i data-feather="key" class="w-5 m-1"></i>
            <b>License:&nbsp;</b>
            {{ license }}
          </div>
          <div class="flex items-center">
            <i data-feather="user" class="w-5 m-1"></i>
            <b>Owner:&nbsp;</b>
            <a :href="owner_link" target="_blank" rel="noopener noreferrer" @click.stop
              class="flex hover:text-secondary duration-75 active:scale-90" title="Owner's profile">

              {{ owner }}
            </a>
          </div>

        </div>
        
        <div v-if="patreon!=''" class="flex items-center">
          <i class="w-5 m-1 pr-2">
            <svg
              xmlns:dc="http://purl.org/dc/elements/1.1/"
              xmlns:cc="http://creativecommons.org/ns#"
              xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
              xmlns:svg="http://www.w3.org/2000/svg"
              xmlns="http://www.w3.org/2000/svg"
              xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
              xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
              width="15"
              height="15"
              id="svg3168"
              version="1.1"
              inkscape:version="0.48.4 r9939"
              viewBox="0 0 541.4375 541.43744"
              sodipodi:docname="Patreon">
              <g
                inkscape:label="Layer 1"
                inkscape:groupmode="layer"
                id="layer1"
                transform="translate(-78.58618,-210.44369)">
                <path
                  inkscape:connector-curvature="0"
                  id="path3204"
                  d="m 349.30488,210.44369 c -149.51545,0 -270.7187,121.20325 -270.7187,270.71875 l 0,270.4687 259.375,0 c 3.7608,0.155 7.5448,0.25 11.3437,0.25 149.5155,0 270.7188,-121.2032 270.7188,-270.7187 0,-149.5155 -121.2033,-270.71875 -270.7188,-270.71875 z"
                  style="fill:#ff5900;fill-opacity:1;stroke:none" />
                <path
                  style="fill:#ffffff;fill-opacity:1;stroke:none"
                  d="m 349.30493,273.28744 c -114.80003,0 -207.875,93.07494 -207.875,207.875 l 0,123.90625 0,83.75 0,62.8125 83.1875,0 0,-270.25 c 0,-68.64109 55.64016,-124.3125 124.28125,-124.3125 68.64109,0 124.28125,55.67141 124.28125,124.3125 0,68.64109 -55.64016,124.28125 -124.28125,124.28125 -25.09566,0 -48.463,-7.45836 -68,-20.25 l 0,89.34375 c 13.09042,8.05513 42.97659,13.74429 78.03125,14.03125 110.32856,-5.03362 198.25,-96.05383 198.25,-207.625 0,-114.80006 -93.07493,-207.875 -207.875,-207.875 z m -8.71875,415.53125 c 2.8876,0.1191 5.80191,0.21875 8.71875,0.21875 3.07049,0 6.11821,-0.087 9.15625,-0.21875 l -17.875,0 z"
                  id="path3192"
                  inkscape:connector-curvature="0" />
              </g>
            </svg>

          </i>
          <b>Patreon:&nbsp;</b>
          <!-- <p class="mx-1 opacity-80 line-clamp-3" :title="description">{{ description }}</p> -->
          <a :href="patreon" class="mx-1 opacity-80 line-clamp-3 overflow-y_auto" :title="description">{{ patreon }}</a>
          <!-- <p class="mx-1 opacity-80 line-clamp-3" :title="description"><span v-html="description"></span></p> -->

        </div>


        <div class="flex items-center">
          <i data-feather="info" class="w-5 m-1"></i>
          <b>Description:&nbsp;</b><br>
        </div>
        <!-- <p class="mx-1 opacity-80 line-clamp-3" :title="description">{{ description }}</p> -->
        <p class="mx-1 opacity-80 line-clamp-3 overflow-y_auto" :title="description">{{ description.replace(/<\/?[^>]+>/ig, " ") }}</p>
        <!-- <p class="mx-1 opacity-80 line-clamp-3" :title="description"><span v-html="description"></span></p> -->


      </div>
    </div>
  </div>

</template>



<script>
import filesize from '../plugins/filesize'
import axios from "axios";
import { nextTick } from 'vue'
import feather from 'feather-icons'
import defaultImgPlaceholder from "../assets/default_model.png"
import InteractiveMenu from "@/components/InteractiveMenu.vue"


const bUrl = import.meta.env.VITE_LOLLMS_API_BASEURL
export default {
  components:{InteractiveMenu},
  props: {
    title: String,
    icon: String,
    path: String,
    owner: String,
    owner_link: String,
    license: String,
    patreon: String,
    description: String,
    isInstalled: Boolean,
    onInstall: Function,
    onCancelInstall: Function,
    onUninstall: Function,
    onSelected: Function,
    onCopy: Function,
    onCopyLink: Function,
    selected: Boolean,
    model: Object,
    model_type: String
  },
  data() {
    return {
      progress: 0,
      speed: 0,
      total_size: 0,
      downloaded_size: 0,
      start_time: '',
      installing: false,
      uninstalling: false,
      failedToLoad: false,
      linkNotValid: false,
      selected_variant: ''
    };
  },
  async mounted() {
    //this.fileSize = await this.getFileSize(this.model.path)
    //console.log('model path', this.model.path)
    nextTick(() => {
      feather.replace()


    })
  },
  methods: {
    formatFileSize(sizeInBytes) {
    if (sizeInBytes < 1024) {
      return sizeInBytes + " bytes";
    } else if (sizeInBytes < 1024 * 1024) {
      return (sizeInBytes / 1024).toFixed(2) + " KB";
    } else if (sizeInBytes < 1024 * 1024 * 1024) {
      return (sizeInBytes / (1024 * 1024)).toFixed(2) + " MB";
    } else {
      return (sizeInBytes / (1024 * 1024 * 1024)).toFixed(2) + " GB";
    }
  },    
    computedFileSize(size) {
      return filesize(size)
    },
    async getFileSize(url) {
      if (this.model_type != "api") {
        try {
          const res = await axios.head(url)
          if (res) {

            if (res.headers["content-length"]) {
              return this.computedFileSize(res.headers["content-length"])
            }
            if (this.model.filesize) {
              return this.computedFileSize(this.model.filesize)
            }
            return 'Could not be determined'

          }
          if (this.model.filesize) {

            return this.computedFileSize(this.model.filesize)
          }
          return 'Could not be determined'

          // Example response
          // {
          //   date: 'Tue, 03 Apr 2018 14:29:32 GMT',
          //   'content-type': 'application/javascript; charset=utf-8',
          //   'content-length': '9068',
          //   connection: 'close',
          //   'last-modified': 'Wed, 28 Feb 2018 04:16:30 GMT',
          //   etag: '"5a962d1e-236c"',
          //   expires: 'Sun, 24 Mar 2019 14:29:32 GMT',
          //   'cache-control': 'public, max-age=30672000',
          //   'access-control-allow-origin': '*',
          //   'cf-cache-status': 'HIT',
          //   'accept-ranges': 'bytes',
          //   'strict-transport-security': 'max-age=15780000; includeSubDomains',
          //   'expect-ct': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"',
          //   server: 'cloudflare',
          //   'cf-ray': '405c3a5cba7a68ba-CDG'
          // }


        } catch (error) {
          console.log(error.message, 'getFileSize')
          //this.linkNotValid = true
          return 'Could not be determined'

        }
      }


    },
    getImgUrl() {

      if (this.icon === '/images/default_model.png') {
        return defaultImgPlaceholder
      }

      return this.icon
    },
    defaultImg(event) {
      event.target.src = defaultImgPlaceholder
    },
    toggleInstall() {

      if (this.isInstalled) {
        this.uninstalling = true;
        // Simulate uninstallation delay (replace this with your WebSocket logic)
        this.onUninstall(this);
      } else {
        //this.installing = true;
        this.onInstall(this);
      }
    },
    toggleSelected(force=false) {
      this.onSelected(this,force)
    },
    toggleCopy() {

      this.onCopy(this)
    },
    toggleCopyLink() {
      this.onCopyLink(this)
      //navigator.clipboard.writeText(this.path)
    },
    toggleCancelInstall() {
      this.onCancelInstall(this)

    },
    handleSelection() {
      if (this.isInstalled && !this.selected) {
        this.onSelected(this);
      }
    },
    copyContentToClipboard() {
      this.$emit('copy', 'this.message.content')

    },
  },
  computed: {
    computed_classes(){
      if(!this.model.isInstalled){
        return 'border-transparent'
      }
      if(this.selected){
        console.log("Selected")
        return 'border-4 border-gray-200 bg-primary'
      }      
      return 'border-0 border-primary bg-primary'
    },
    commandsList(){
      let main_menu = [
                {name:this.model.isInstalled?"Uninstall":"Install", icon: "feather:settings", is_file:false, value:this.toggleInstall},
                {name:"Copy model info to clipboard", icon: "feather:settings", is_file:false, value:this.toggleCopy},
              ];
        if(this.selected){
          main_menu.push({name:"Reload", icon: "feather:refresh-ccw", is_file:false, value:this.toggleSelected})
        }
        return main_menu
      },  
      selected_computed(){
        return this.selected
    },    
    fileSize: {
        get() {
          // console.log(this.model)
          if (this.model && this.model.variants && this.model.variants.length > 0) {
            const sizeInBytes = this.model.variants[0]["size"];
            return this.formatFileSize(sizeInBytes);
          }
          return null; // Return null if the conditions are not met
        },
    },
    speed_computed() {
      return filesize(this.speed)
    },
    total_size_computed() {
      return filesize(this.total_size)
    },
    downloaded_size_computed() {
      return filesize(this.downloaded_size)
    },

  },
  watch: {
    linkNotValid() {
      nextTick(() => {
        feather.replace()


      })
    }
  }
};
</script>
