/**
 * MIT License
 *
 * Copyright (c) 2020 ManBang Group
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

import threshApp from '../..'
import ChildrenRule from './ChildrenRule'
import scheduleUpdate from './scheduleUpdate'
import VNode from './VNode'
import Util from '../shared/Util'
import { RefCallback, ThreshWidget } from '../types/widget'
import LinkedController from '../coolink-base/LinkedController'
import { isTv as isTvFunc } from '../shared/cooInfo';

/**
 * 校验object是否合法
 * @param {any} object 
 */
function checkObjectValid (object: any, noFunc: boolean = false): boolean {
  if (object === null || object === undefined) return true
  if (!Util.isObject(object)) {
    threshApp.onError(new Error('props or state must be an object type'))
    return false
  }
  if (noFunc) {
    for (const key in object) {
      if (Util.isFunc(object[key])) {
        threshApp.onError(new Error(`prop ${key} must not be a function type`))
        return false
      }
    }
  }
  return true
}

/**
 * Widget
 */
// interface WidgetProps {
//   key?: any,
//   ref?: RefCallback,
//   children?: ThreshWidget[],
// }
export default class Widget <P = {}, S = {}> {
  props: P
  state?: S
  __vNode__: VNode
  controller: LinkedController = null

  static defaultProps: any = {}

  widgetDidMount() {}
  widgetDidUpdate() {}
  widgetDidUnmount() {}

  pageDidPush(){}
  pageDidPop(){}
  pageDidPopNext(){}
  pageDidPushNext(){}

  constructor (props: P) {
    if (checkObjectValid(props)) {
      this.props = Util.merge(props) as any
    }
  }
  
  /**
   * 更新state
   * @param {any} state
   */
  setState (state?: S) {
    if (!this.__vNode__.hasMount || !checkObjectValid(state)) return
    if (state) Object.assign(this.state, state)
    scheduleUpdate(this.__vNode__)
  }
  /**
   * 强制更新
   */
  forceUpdate() {
    this.setState()
  }

  /**
   * 组件渲染方法
   */
  render (): VNode | void {}

  /**
   * 判断当前设备是否是TV
   * @returns true/false
   */
  isTv() {
    return isTvFunc();
  }

  /**
   * 启用联动
   * @param linkName 
   */
  enableLinking(linkName: string) {
    this.controller = LinkedController.Create(linkName);
    if (this.controller) {
      this.controller.setOnStateCallback(this.setState.bind(this));
    }
  }

  /**
   * 停止联动
   */
  disableLinking() {
    if (this.controller) {
      this.controller.disableLinking();
      this.controller = null;
    }
  }

  /**
   * 发送TEXT命令
   */
  sendLinkCommand(obj: any) {
    if (this.controller) {
      this.controller.sendLinkCommand(obj);
    }
  }

  /**
   * 设置:收到对方设备的联动命令的回调
   */
  setLinkCommandCallback(cb: Function) {
    if (this.controller) {
      this.controller.setLinkCommandCallback(cb);
    }
  }

  /**
   * 设置 TV端的State
   */
  setTvState(state: any) {
    if (this.isTv()) {
      this.setState(state);
    } else {
      this.sendLinkCommand({
        cmd: 'set_remote_state',
        value: state
      });
    }
  }

  /**
   * 设置 TV端的State
   */
  setMobileState(state: any) {
    if (this.isTv()) {
      this.sendLinkCommand({
        cmd: 'set_remote_state',
        value: state
      });
    } else {
      this.setState(state);
    }
  }
}

/**
 * 基础组件类
 * 继承自Widget
 * 外部不可使用该类
 */
export class BasicWidget <P = {}, S = {}> extends Widget <P, S> {
  static isBasicWidget: boolean = true
  static childrenRule: ChildrenRule

  setState () {
    threshApp.onError(new Error(`basic widget cannot call setState()`))
  }
}
