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

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

  BASE_URL = environment.BASE_URL;

  notify: boolean = false;
  shareIt: boolean = false;

  line1: any = "";
  line2: any = "";
  line3: any = "";

  line1_number = 0;
  line2_number = 0;
  line3_number = 0;

  five_max: any;
  five_id_list = [];
  seven_max: any;
  seven_id_list = [];

  ready = false;

  constructor(
    private toastr: ToastrService,
    private _clipboardService: ClipboardService,
    private data: DataService
  ) {
    axios.get(this.BASE_URL + "/ping")
    .catch(console.error);
   }

  ngOnInit() {
    try {
      axios.get(this.BASE_URL + "/get_line5_ids")
      .then((the) => {
        this.five_id_list = the['data'];
        this.five_max = the['data'].length;
        return axios.get(this.BASE_URL + "/get_line7_ids");
      })
      .catch((e) => {
        this.toastr.error('Server Error!', e);
      })
      .then((the: any) => {
        this.seven_id_list = the['data'];
        this.seven_max = the['data'].length;
        console.log("FIVE MAX: ", this.five_max);
        console.log("SEVEN MAX: ", this.seven_max);
        return this.instance_generator()
      })
      .then(() => {
        console.log("INSTANCE LOADED");
        this.ready = true;
      })
      .catch((e) => {
        console.error(e);
        this.toastr.error('Server Error!', e);
      })
    } catch (e) {
      console.log(e);
    }
  }

  instance_generator() {
    
    this.line1_number = this.five_id_list[Math.floor(Math.random() * this.five_max) + 1];
    this.line2_number = this.seven_id_list[Math.floor(Math.random() * this.seven_max) + 1];
    this.line3_number = this.five_id_list[Math.floor(Math.random() * this.five_max) + 1];

    axios({
      method: 'post',
      url: this.BASE_URL + '/get_instance', 
      params: {
        line1: this.line1_number,
        line2: this.line2_number,
        line3: this.line3_number
    }})
    .then((ting) => {
      this.line1 = ting.data.line1;      
      this.line2 = ting.data.line2;      
      this.line3 = ting.data.line3;      
    })
    .catch((e) => {
      console.log(e);
      this.toastr.error('Server Error!', e);
    });
  }

  copy_text(_text: any) {
    let url = `https://udohaiku.com/#/share/${this.line1_number}/${this.line2_number}/${this.line3_number}`
    this._clipboardService.copyFromContent(url);
    this.toastr.info('URL Copied To Clipboard ');
  }

  async saveInstance() {
    try {
      await this.data.set_item({
        line1: this.line1,
        line2: this.line2,
        line3: this.line3,
        line_number1: this.line1_number,
        line_number2: this.line2_number,
        line_number3: this.line3_number
      });
      this.toastr.success('Instance Saved !', "");
    } catch (e) {
      console.error(e);
      this.toastr.error('Unable to save instance !');
    }
  }

}
