import image from '@ohos.multimedia.image';
import fs from "@ohos.file.fs"
import common from '@ohos.app.ability.common';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import commonEvent from "@ohos.commonEvent";
import CommonEventManager from '@ohos.commonEventManager'
import uploadInstance from '../utils/UploadUtil';
import router from '@ohos.router';

const TAG = "openHarmonyBridge upload"
@Entry
@Component
struct Index {

  @State isUploading: boolean = false
  /*相机需要启动lib脚本
   * hdc shell mount -o remount,rw /vendor
   * */
  @State commodityPixelMap: PixelMap | null = null;
  @State isButtonPressed: boolean = false;
  @State isCapturing: boolean = false; // 新增状态控制拍摄状态
//
  subscriber: CommonEventManager.CommonEventSubscriber | null = null; // Used to save the created subscriber object for subsequent subscription and unsubscription.
  // Subscriber information.
  subscribeInfo: CommonEventManager.CommonEventSubscribeInfo = { events: ["sonycamera_callback"] };
  getPermission = async () => {
    let context = getContext(this) as common.UIAbilityContext;
    let atManager = abilityAccessCtrl.createAtManager();
    try {
      atManager.requestPermissionsFromUser(context, ['ohos.permission.WRITE_IMAGEVIDEO', 'ohos.permission.READ_IMAGEVIDEO'])
        .then((data) => {
          console.log('data: ' + JSON.stringify(data));
        })
        .catch((err: object) => {
          console.log('err: ' + JSON.stringify(err));
        })
    } catch (err) {
      console.log('catch err->' + JSON.stringify(err));
    }
  }
  //订阅回调（拍照完接收到照片收到回调）
  createSubscriber = async () => {
    this.subscriber = await CommonEventManager.createSubscriber(this.subscribeInfo);
    if (this.subscriber) {
      console.info("[CommonEvent] Subscriber created successfully");
      CommonEventManager.subscribe(this.subscriber, (err, data) => {
        if (err?.code) {
          console.error("[CommonEvent] SubscribeCallBack err=" + JSON.stringify(err));
        } else {
          console.info("[CommonEvent] SubscribeCallBack data=" + JSON.stringify(data));
          if (data && data.code&&data.code === 2) {
            this.handleCaptureSuccess()
          }
        }
      });
    }
  };

  handleCaptureSuccess = () => {
    this.savePhoto();
    commonEvent.publish("startview", (err) => {
      if (err?.code) {
        console.error("Restart preview error:", JSON.stringify(err));
      } else {
        this.isCapturing = false; // 拍摄完成后恢复预览
        console.log("Restart success")
      }
    });
  };

  connectCamera = async () => {
    commonEvent.publish("opensession", (err) => {
      if (err?.code) {
        console.info("[CommonEvent] Publish CallBack err=" + JSON.stringify(err))
      } else {
        commonEvent.publish("startview", (err) => {
          console.info("[CommonEvent] Publish opensession succeed ")
          if (err?.code) {
            console.info("[CommonEvent] Publish CallBack err=" + JSON.stringify(err))
          } else {
            console.info("[CommonEvent] Publish startview succeed ")
          }
        })
      }
    })
  }
  disconnectCamera = async () => {
    commonEvent.publish("stopview", (err) => {
      if (err?.code) {
        console.info("[CommonEvent] Publish CallBack err=" + JSON.stringify(err))
      } else {
        console.info("[CommonEvent] Publish stopview succeed ")
        commonEvent.publish("closesession", (err) => {
          if (err?.code) {
            console.info("[CommonEvent] Publish CallBack err=" + JSON.stringify(err))
          } else {
            console.info("[CommonEvent] Publish closesession succeed ")
          }
        })
      }
    })
  }
  savePhoto=async()=>{
    const context = getContext() as common.UIAbilityContext;
    console.log(TAG, "进入了savephoto")
    try {
      // 获取相册访问的 helper 实例
      const helper = photoAccessHelper.getPhotoAccessHelper(context);

      console.log(TAG, "进入了savephoto1")
      // 创建一个新的图片资产，并获取目标 URI
      // const dest = await helper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'jpg');

      // console.log(TAG, "进入了savephoto2")
      // 打开源文件和目标文件  这个是写死的 app的名字不能改
      let imageUri: string = "/data/storage/el2/base/haps/entry/files/shoots.jpg"
      // 开始上传
      console.log(TAG, "开始上传", imageUri)
      uploadInstance.startUpload(imageUri, () => {

        console.log(TAG, "上传完成后的callback")
        this.isUploading = false
      })


      // let res = fs.accessSync(imageUri);
      // console.info("demo1"+ "file " + imageUri)
      // if (res) {
      //   console.info("demo1"+"file exists");
      // } else {
      //   console.info("demo1"+"file not exists");
      // }
      // const srcFile = fs.openSync(imageUri);
      // const destFile = fs.openSync(dest, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
      // fs.copyFileSync(srcFile.fd, destFile.fd);
      // fs.closeSync(srcFile);
      // fs.closeSync(destFile);
      // console.log("demo1"+'Image saved to album successfully:', dest);
    } catch (error) {
      console.log(TAG, "进入了savephoto3")
      console.error("demo1"+'Failed to save image to album:', error.code);
    }
  }
  takePhoto = async () => {
    this.isCapturing = true;
    commonEvent.publish("stopview", (err) => {
      if (err?.code) {
        console.info("demo1"+"[CommonEvent] Publish CallBack err=" + JSON.stringify(err));
        this.isCapturing = false;
      } else {
        console.info("demo1"+"[CommonEvent] Publish stopview succeed");
        commonEvent.publish("shootonly", (err) => {
          if (err?.code) {
            console.info("demo1"+"[CommonEvent] Publish CallBack err=" + JSON.stringify(err));
            this.isCapturing = false;
          } else {
            console.info("demo1"+"[CommonEvent] Publish shootonly succeed");
          }
        });
      }
    });
  };
  liveShow = async () => {
    const context: Context = getContext(this);
    let index = 0;
    while (true) {
      if(!this.isCapturing) {
        const filePath: string = `${context.filesDir}/live${index}.jpg`;
        console.info("icamera"+filePath)
        try {
          const imageSource = image.createImageSource(filePath);
          if (imageSource) {
            this.commodityPixelMap = await imageSource.createPixelMap();
          }
          console.info("demo"+"file exists");
        } catch (error) {
          console.info("demo"+"file not exists");
        }
        index = (index + 1) % 10;
      }
      await sleep(50);
    }
  }

  aboutToAppear(): void {
    this.createSubscriber();
    this.connectCamera();
    this.getPermission();
    this.liveShow();
  }

  aboutToDisappear(): void {
    this.disconnectCamera();
    if (this.subscriber) {
      CommonEventManager.unsubscribe(this.subscriber);
    }
  }

  build() {
    Column() {
      Row() {

      }
      .width('100%')
      .height('5%')
      .alignItems(VerticalAlign.Center)
      Row() {
        if (this.commodityPixelMap) {
          Stack({ alignContent: Alignment.Center }) {
            Image(this.commodityPixelMap)
              .width('100%')
              .height('100%')
              .objectFit(ImageFit.Contain)

            if (this.isUploading) {
              Column() {
                Text('正在上传中...').fontSize(22).margin({ top: 10, bottom: 10 })
                LoadingProgress()
                  .color(Color.Blue)
                  .layoutWeight(1)
              }
              .height(140)
              .width(150)
              .backgroundColor(Color.White)
              .borderRadius(10)
            }

          }

        }
      }
      .width('100%')
      .height('80%')
      .backgroundColor('#000000') // 背景为黑色

      // 底部：拍照按钮区域
      Row() {
        Button('', { type: ButtonType.Circle})
          .width(this.isButtonPressed ? 70 : 80) // 按下时缩小按钮
          .height(this.isButtonPressed ? 70 : 80)
          .backgroundColor(this.isButtonPressed ? '#CCCCCC' : '#FFFFFF') // 按下时按钮颜色变灰
          .border({ width: 4, color: '#FFFFFF' })// 白色边框
          .onClick(() => {
            console.log(TAG, "点击了拍照按钮")
            if(this.isUploading){
              return
            }
            this.isUploading = true
            this.takePhoto();

          })
          .onTouch((event: TouchEvent) => {
            if(this.isUploading){
              return
            }
            if (event.type === TouchType.Down) {
              this.isButtonPressed = true; // 按下时设置为 true
            } else if (event.type === TouchType.Up || event.type === TouchType.Cancel) {
              this.isButtonPressed = false; // 松开时设置为 false
            }
          })
        Button(){
          Image($r('app.media.close'))
            .width(40)
            .height(40)
        }
        .width(70)
        .height(70)
        .backgroundColor('#1c1c1c')
        .onClick(() => {
          router.back()        })

      }.width('100%')
      .height('15%')
      .justifyContent(FlexAlign.SpaceEvenly) // 按钮居中
      .backgroundColor('#000000') // 底部背景为黑色
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#000000')
  }

  // 整体背景为黑色
  /* Text("sada")
        .onClick(async () => {
          console.info("sssss");
          // 异步加载图片
          const context: Context = getContext(this);
          let index = 0; // 初始化索引
          while (true) { // 无限循环
            const filePath: string = `${context.filesDir}/live${index}.jpg`;
            try {
              fs.accessSync(filePath); // 检查文件是否存在
              console.info("file " + filePath);
              const imageSource = image.createImageSource(filePath);
              if (imageSource) {
                this.commodityPixelMap = await imageSource.createPixelMap();
              }
              console.info("file exists");
            } catch (error) {
              console.info("file not exists");
            }
            index = index  % 10; // 更新索引，循环从0到9
            await sleep(100);
          }


        }).width('20%')
        .height('10%')
      Text("nihao")
        .onClick(async () => {
          const context = getContext() as common.UIAbilityContext;
          try {
            // 获取相册访问的 helper 实例
            const helper = photoAccessHelper.getPhotoAccessHelper(context);

            // 创建一个新的图片资产，并获取目标 URI
            const dest = await helper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'jpg');

            // 打开源文件和目标文件
            let imageUri: string = "/data/storage/el2/base/haps/entry/files/shoot.jpg"
            let res = fs.accessSync(imageUri);
            console.info("file " + imageUri)
            if (res) {
              console.info("file exists");
            } else {
              console.info("file not exists");
            }
            const srcFile = fs.openSync(imageUri);
            console.info("file 1")
            const destFile = fs.openSync(dest, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
            console.info("file 2")
            // 将源文件内容复制到目标文件
            fs.copyFileSync(srcFile.fd, destFile.fd);
            console.info("file 3")
            // 关闭文件句柄
            fs.closeSync(srcFile);
            fs.closeSync(destFile);

            console.log('Image saved to album successfully:', dest);

          } catch (error) {
            console.error('Failed to save image to album:', error.code);
            throw new Error(`Failed to save image to album: ${error.message}`);
          }
        })

        .width('100%')
        .height('100%')
        .backgroundColor('#F1F3F5')
    }
  }
}*/
}
function sleep(ms: number): Promise<void> {
  return new Promise((resolve) => setTimeout(resolve, ms));
}


