import { Component, OnInit } from '@angular/core';
import { DataService } from '../data.service';
import { ToastrService } from 'ngx-toastr';
import { ClipboardService } from 'ngx-clipboard'

@Component({
  selector: 'app-saved',
  templateUrl: './saved.component.html',
  styleUrls: ['./saved.component.scss']
})
export class SavedComponent implements OnInit {

  shareIt: boolean = false;
  sharing_id: string = "";


  constructor(
    private toastr: ToastrService,
    private data: DataService,
    private _clipboardService: ClipboardService
  ) { }

  instance_list = [];

  ngOnInit(): void {
    this.instance_list = [];
    this.data.get_all()
    .then((data: any) => {
      this.instance_list = data;
      console.log("instance_list ", this.instance_list);
      
    })
    .catch((e) => {
      console.log(e);
    })
  }

  delete_item(_item: any) {
    this.data.remove_item(_item)
    .then(() => {
      this.toastr.success("Instance Deleted !")
      this.ngOnInit();
    })
    .catch(console.error);
  }

  copy_text(_line1: any, line2: any, line3: any) {
    let url = `https://udohaiku.com/#/share/${_line1}/${line2}/${line3}`
    this._clipboardService.copyFromContent(url);
    this.toastr.info('URL Copied To Clipboard ');
    this.sharing_id = "";
    this.shareIt = !this.shareIt;
  }

  share_it(_id: string) {
    this.sharing_id = _id;
    this.shareIt = !this.shareIt;
  }

}
