// noPage
import esp from 'esoftplay/esp';

import { LibComponent } from 'esoftplay/cache/lib/component/import';
import { LibCurl } from 'esoftplay/cache/lib/curl/import';
import { LibProgress } from 'esoftplay/cache/lib/progress/import';
import { LibSlidingup } from 'esoftplay/cache/lib/slidingup/import';
import { LibTextstyle } from 'esoftplay/cache/lib/textstyle/import';
import { LibToastProperty } from 'esoftplay/cache/lib/toast/import';
import { MarketButton } from 'esoftplay/cache/market/button/import';
import { MarketInput_label } from 'esoftplay/cache/market/input_label/import';
import { MarketInput_square } from 'esoftplay/cache/market/input_square/import';
import { MarketStyle } from 'esoftplay/cache/market/style/import';

import React from 'react';
import { ScrollView, View } from 'react-native';

export interface MarketReviewProps {
}
export interface MarketReviewState {
  text: string,
  review: string
}

export default class m extends LibComponent<MarketReviewProps, MarketReviewState> {

  dialogReview = React.createRef<LibSlidingup>()
  text: string = ""
  url: string = ""
  callback: any

  constructor(props: MarketReviewProps) {
    super(props);
    this.doReview = this.doReview.bind(this);
    this.show = this.show.bind(this);
    this.hide = this.hide.bind(this);
    this.state = { text: '', review: '' }
  }

  show(review: string, url: string, callback: () => void): void {
    this.callback = callback
    this.url = url
    this.dialogReview.current?.show()
    this.setState({ review })
  }

  hide(): void {
    this.dialogReview.current?.hide()
  }

  doReview(): void {
    if (this.url) {
      LibProgress.show(esp.lang("market/review", "review_wait"))
      new LibCurl(this.url, { notes: this.state.text },
        (res, msg) => {
          this.hide()
          this.callback()
          LibProgress.hide()
        },
        (msg) => {
          LibToastProperty.show(msg?.message)
        }, 1
      )
    }
  }

  render(): any {
    const { text, review } = this.state
    return (
      <LibSlidingup ref={this.dialogReview}>
        <View style={{ backgroundColor: 'white', borderTopRightRadius: 20, borderTopLeftRadius: 20, paddingHorizontal: 12, paddingTop: 20, paddingBottom: 20 }}>
          <LibTextstyle text={esp.lang("market/review", "label_send_review")} textStyle={"headline"} style={{ textAlign: "center", marginBottom: 16 }} />
          <MarketInput_label label={esp.lang("market/review", "msg_from_admin")} />
          <ScrollView showsVerticalScrollIndicator={false} style={{ maxHeight: 200 }} >
            <LibTextstyle textStyle={"body"} text={review} style={{ marginHorizontal: 4, marginBottom: 10 }} />
          </ScrollView>
          <MarketInput_square
            mandatory
            containerStyle={{ marginHorizontal: -17 }}
            multiline
            autoFocus
            style={{ height: 100, textAlignVertical: "top" }}
            label={esp.lang("market/review", "label_notes")} onChangeText={(t) => this.setState({ text: t })} />
          <View pointerEvents={text.length < 3 ? "none" : "auto"} >
            <MarketButton
              colorBackground={text.length < 3 ? "#aaa" : MarketStyle.colorPrimaryMarket}
              buttonStyle={{ marginTop: 20 }}
              text={esp.lang("market/review", "btn_save")}
              onPress={() => this.doReview()}
            />
          </View>
        </View>
      </LibSlidingup>
    )
  }
}