import Api from './api'
import { Platform } from 'react-native'
import Config from 'src/utils/Config'
import DeviceInfo from 'react-native-device-info'
import ApiPowerCondo from "./apiPowerCondo";
import moment from "moment";

export const getUserProfile = () => {
  return Api.get('/mobile/me');
}

export const testConnect = () => {
  return Api.get('/');
}

export const registerDevice = (pushToken) => {
  return Api.post('/mobile/register/device', {
    push_token: pushToken,
    uid: DeviceInfo.getUniqueId(),
    brand: DeviceInfo.getBrand(),
    model: DeviceInfo.getModel()
  });
}

export const register = (user) => {
  const formData = new FormData()
  for (let k in user) {
    formData.append(k, user[k])
  }
  return Api.post('/register', formData );
}

export const login = (params) => {
  params = {
    ...params,
    uid: DeviceInfo.getUniqueId(),
    brand: DeviceInfo.getBrand(),
    model: DeviceInfo.getModel()
  }

  return Api.post('/api/login',
    params
  );
}

export const logout = () => {
  return Api.post('/api/logout');
}

export const dashboard = (params) => {
  return Api.get('/mobile/me/dashboard');
}

export const getNews = (params) => {
  return Api.get('/mobile/news');
}

export const getNewsPage = (page = 1) => {
  return Api.get('/mobile/news?page='+page);
}

export const getNewsDetail = (news_id) => {
  return Api.get('/mobile/news/'+news_id);
}

export const getNewsByProject = (project_id) => {
  return Api.get('/mobile/news-projects/'+project_id);
}

export const project = (keyword) => {
  return Api.get('/mobile/project');
}

export const getBuildingByProjectId = (project_id) => {
  return Api.get(`/mobile/project/${project_id}/building`);
}

export const room = (project_id) => {
  return Api.get(`/room/?project_id=${project_id}`);
}

export const roomdetail = (room_id) => {
  return Api.get('/room_detail', {
    room_id: room_id
  });
}

export const payment = (user_id, page, search='') => {
    return Api.get('/mobile/me/invoice', {
        user_id,
        page,
        search
    });
}

export const payment_detail = (payments_id) => {
  return Api.get('/mobile/payment/'+payments_id);
}

export const total_payment = (user_id) => {
  return Api.get('/payment/' + user_id);
}

export const edit_profile = (user) => {
  // const formData = new FormData()
  // for (let k in user) {
  //   formData.append(k, user[k])
  // }
  // let formDataParts = formData._parts ? formData._parts : formData;
  return Api.post('/mobile/me/profile', user );
}

export const get_notification = (user_id, page) => {
  return Api.get('/mobile/notification', {
    user_id: user_id,
    page: page
  })
}

export const read_notification = (noti_id) => {
  return Api.post('/mobile/notification',{noti_id: noti_id})
}

export const count_notification = (user_id) => {
  return Api.get('/mobile/notification/count', {
    user_id: user_id
  })

}

export const getSubscription = (customer_id,type) => {
  return Api.get('/mobile/subscription', {
    customer_id: customer_id,
    type: type,
  })
}

export const postSubscription = (customer_id,type) => {
  return Api.post('/mobile/subscription', {
    customer_id: customer_id,
    type: type,
  })
}

export const getProductList = () => {
  return Api.get('/mobile/product');
}

export const getProductDetail = (product_id) => {
  return Api.get('/mobile/product/'+product_id);
}

export const getServices = () => {
  return Api.get('/service');
}

export const getServicesType = (service_id) => {
  return Api.get('/service/type/'+service_id);
}

export const getServicesDetail = (service_id) => {
  return Api.get('/service/'+service_id);
}

export const getRepairList = (project_id) => {
  return Api.get(`/api/project/${project_id}/maintenance`);
}

export const getRepairById = (project_id, id) => {
  return Api.get(`/api/project/${project_id}/maintenance/${id}`);
}

export const postRepair = (param) => {
  return Api.post('/repair',param)
}

export const postReserveRoom = (room_id,params) => {
  return Api.post(`/room/${room_id}/reserve`, params)
}

//Suggestion
export const postSuggestion = (params) => {
  const formData = new FormData()
  for (let k in params) {
    formData.append(k, params[k])
  }
  let newFormData = formData._parts ? formData._parts : formData;
  return Api.post('/mobile/suggestion', newFormData)
}

export const postSuggestionLogin = (params) => {
  const formData = new FormData()
  for (let k in params) {
    formData.append(k, params[k])
  }
  return Api.post('/mobile/service-suggestion',formData)
}


//Question & Answer
export const getConversation = () => {
  return Api.get('/mobile/conversation');
}

export const postConversation = (param) => {
  return Api.post('/mobile/conversation/messages',param);
}

//Object & Message
export const getObjectMessage = () => {
  return Api.get('/mobile/parcel');
}

export const postSignatureImage = (params) => {
  const formData = new FormData()
  for (let k in params) {
    formData.append(k, params[k])
  }
  return Api.post('/mobile/parcel',formData);
}

export const getObjectMessageByID = (id) => {
  return Api.get(`/mobile/parcel/${id}`);
}

//Meter
export const getMeterList = () => {
  return Api.get('/mobile/meter');
}

export const getRoomSlideList = (roon_id) => {
  return Api.get('/mobile/meter?room_id='+roon_id);
}

export const postMeter = (params) => {
  // const formData = new FormData()
  // for (let k in params) {
  //   formData.append(k, params[k])
  // }

  return Api.post('/mobile/meter',params);
}

export const postMeterImage = (params) => {
  const formData = new FormData()
  for (let k in params) {
    formData.append(k, params[k])
  }

  return Api.post('/mobile/ocr',formData);
}

export const disconnectFacebook = (user) => {
  return Api.post('/disconnectFacebook',
    user
  );
}

export const getRoomParcel = (room_no) => {
  return Api.get('/check_room_parcel?room_no='+room_no);
}

export const setPaymentLater = () => {
  return Api.get('/set_payment_later')
}

export const setFillInMeterLater = () => {
  return Api.get('/set_record_meter_later')
}

export const checkPaymentMeter = () => {
  return Api.get('/mobile/me/check-payment-meter')
}

export const getRoomUser = () => {
  return Api.get('/mobile/get_room_user');
}

export function cancelRepair(id) {
  return Api.post('/repair/cancel', {id: id})
}

export function postEvaluateRepair(params) {
  return Api.post('/repair/point', params)
}

export function setLanguage(language) {
  return Api.post('/mobile/me/set-language', {language: language})
}

export function getRoomPowerCondo(project_id, customer_id) {
  return Api.get(`/api/project/${project_id}/room/list/customer?customer_id=${customer_id}`)
}

export function getRoomSelfPowerCondo() {
  return Api.get(`/mobile/me/room`)
}

export function getRepairDropdownList(query) {
  return Api.get(`/api/dropdowns?name=${query}`)
}

export function getRepairListPowerCondo(project_id) {
  return Api.get(`/api/project/${project_id}/maintenance-setting`)
}

export function sendRequestRepair(project_id, body){
  return Api.post(`/api/project/${project_id}/maintenance`, body)
}

export function sendMoveOutData(project_id, body){
  return Api.post(`/api/project/${project_id}/move-out`, body)
}

export function getListReward(category_id='') {
  category_id = category_id ? category_id : '';
  return Api.get(`/mobile/reward?category_id=${category_id}`)
}

export function getRewardDetail(id, is_my_reward) {
  let url = is_my_reward ? '/mobile/me/reward' : '/mobile/reward';
  return Api.get(`${url}/${id}`)
}

export function getListMyReward(type_id = '', category_id = '', customer_finance_id='') {
  return Api.get(`/mobile/me/reward?type=${type_id}&category_id=${category_id}&customer_finance_id=${customer_finance_id}`)
}

export function getHistoryRedeemPoint(datetime) {
  datetime = moment(datetime, 'MMMM YYYY')
  return Api.get(`/mobile/me/history-point?month=${datetime.format('M')}&year=${datetime.format('YYYY')}`)
}

export function redeemReward(id) {
  return Api.post(`/mobile/redeem/${id}`)
}

export function scanReward(id, code) {
  return Api.post(`/mobile/coupon/${id}/qr/${code}`)
}

export function usedCoupon(id) {
  return Api.post(`/mobile/coupon/${id}/qr`)
}

export function useMyReward(id) {
  return Api.post(`/mobile/me/reward/${id}`)
}

export function forgetPassword(mobile) {
  return Api.post('/api/forget-password', {mobile});
}

export function sendVerifyOTP(token, otp) {
  return Api.post(`/api/verify-otp/${token}`, {otp})
}

export function getRewardTermAndCondition(reward_id) {
  return Api.get(`/mobile/reward/${reward_id}/term-and-condition`);
}

export function useRentalDiscount(coupon_id, payment_id){
  return Api.post(`/mobile/coupon/${coupon_id}/discount/${payment_id}`)
}

export function removeRentalDiscount(coupon_id, payment_id){
  return Api.delete(`/mobile/coupon/${coupon_id}/discount/${payment_id}`)
}

export function getLevelMembership(){
  return Api.get(`/mobile/me/loyalty`);
}

export function getLevelMemberDetail(){
  return Api.get(`/mobile/loyalty/privilege`)
}

export function getPointExpire(){
  return Api.get('/mobile/me/expired-point')
}
export function requestDeleteAccount() {
  return Api.get('/me/request_delete')
}
