
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { tap, map } from 'rxjs/operators';
import { HttpErrorResponse } from '@angular/common/http';

import { Ng6UdmMockService } from 'ng6-udm-mock';
import { UdmPost, UdmComment, UdmAlbum, UdmPhoto, UdmTodo,
  UdmGeo, UdmAddress, UdmCompany, UdmUser } from 'ng6-udm-mock';

import { environment } from '../environments/environment';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'app';

  private posts: UdmPost[];
  private post: UdmPost;
  private comments: UdmComment[];
  private comment: UdmComment;
  private albums: UdmAlbum[];
  private album: UdmAlbum;
  private photos: UdmPhoto[];
  private photo: UdmPhoto;
  private todos: UdmTodo[];
  private todo: UdmTodo;
  private users: UdmUser[];
  private user: UdmUser;

  constructor(private sv: Ng6UdmMockService) {

  }

  ngOnInit() {
    this.sv.setBaseUrl(environment.udmMockBaseUrl);
  }
// ============== Post ==================

  public GetAllPosts() {
    this.sv.getAllPosts()
      .subscribe(
        dt => {
          this.posts = dt;
        },
        (err: HttpErrorResponse) => {
          if (err.error instanceof Error) {
            console.log('An error occurred:', err.error.message);
          } else {
            console.log('Status code:', err.status);
            console.log('Response body:', err.error);
          }
        },
        () => {
          console.log('Done successfully');
        }
      );
  }

  public GetPost(id: number) {
    this.sv.getPost(id)
    .subscribe(
      dt => {
        this.post = dt;
      },
      (err: HttpErrorResponse) => {
        if (err.error instanceof Error) {
          console.log('An error occurred:', err.error.message);
        } else {
          console.log('Status code:', err.status);
          console.log('Response body:', err.error);
        }
      },
      () => {
        console.log('Done successfully');
      }
    );
  }

  public GetPostsByUserId(userId: number) {
    this.sv.getPostsByUserId(userId)
    .subscribe(
      dt => {
        this.posts = dt;
      },
      (err: HttpErrorResponse) => {
        if (err.error instanceof Error) {
          console.log('An error occurred:', err.error.message);
        } else {
          console.log('Status code:', err.status);
          console.log('Response body:', err.error);
        }
      },
      () => {
        console.log('Done successfully');
      }
    );
  }

  public AddPost(userId: number, title: string, body: string) {
    this.sv.addPost(userId, title, body)
    .subscribe(
      dt => {
        this.post = dt.body;
      },
      (err: HttpErrorResponse) => {
        if (err.error instanceof Error) {
          console.log('An error occurred:', err.error.message);
        } else {
          console.log('Status code:', err.status);
          console.log('Response body:', err.error);
        }
      },
      () => {
        console.log('Done successfully');
      }
    );
  }

  public UpdatePost() {
    const post: UdmPost = {
      id: 4,
      userId: 1,
      title: 'test01 - updated 01',
      body: 'This is updated text'
    };
    this.sv.updatePost(post)
    .subscribe(
      dt => {
        console.log(dt);
      },
      (err: HttpErrorResponse) => {
        if (err.error instanceof Error) {
          console.log('An error occurred:', err.error.message);
        } else {
          console.log('Status code:', err.status);
          console.log('Response body:', err.error);
        }
      },
      () => {
        console.log('Done successfully');
      }
    );
  }

  public DeletePost() {
    this.sv.deletePost(4)
    .subscribe(
      dt => {
        console.log(dt);
      },
      (err: HttpErrorResponse) => {
        if (err.error instanceof Error) {
          console.log('An error occurred:', err.error.message);
        } else {
          console.log('Status code:', err.status);
          console.log('Response body:', err.error);
        }
      },
      () => {
        console.log('Done successfully');
      }
    );
  }

