import React, { useEffect, useState } from "react";
import { useForm } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import * as yup from "yup";
import MkdSDK from "@/utils/MkdSDK";
import { useNavigate, useParams } from "react-router-dom";
import { GlobalContext, showToast } from "@/context/Global";
import { AuthContext, tokenExpireError } from "@/context/Auth";
import { isImage, empty, isVideo, isPdf } from "@/utils/utils";
import ReactQuill from "react-quill";
import "react-quill/dist/quill.snow.css";

let sdk = new MkdSDK();

const EditSettingPage = () => {
  const { dispatch } = React.useContext(AuthContext);
  const schema = yup
    .object({
      setting_key: yup.string(),
      setting_value: yup.string(),
    })
    .required();
  const { dispatch: globalDispatch } = React.useContext(GlobalContext);
  const [fileObj, setFileObj] = React.useState({});
  const navigate = useNavigate();

  const [setting_key, setSettingKey] = useState("");
  const [setting_value, setSettingValue] = useState("");
  const [id, setId] = useState(0);
  const {
    register,
    handleSubmit,
    setError,
    setValue,
    formState: { errors },
  } = useForm({
    resolver: yupResolver(schema),
  });

  const params = useParams();

  useEffect(function () {
    (async function () {
      try {
        sdk.setTable("setting");
        const result = await sdk.callRestAPI({ id: Number(params?.id) }, "GET");
        if (!result.error) {
          setValue("setting_key", result.model.setting_key);
          setValue("setting_value", result.model.setting_value);

          setSettingKey(result.model.setting_key);
          setSettingValue(result.model.setting_value);
          setId(result.model.id);
        }
      } catch (error) {
        console.log("error", error);
        tokenExpireError(dispatch, error.message);
      }
    })();
  }, []);

  const previewImage = (field, target) => {
    let tempFileObj = fileObj;
    tempFileObj[field] = {
      file: target.files[0],
      tempURL: URL.createObjectURL(target.files[0]),
    };
    setFileObj({ ...tempFileObj });
  };

  const onSubmit = async (_data) => {
    try {
      sdk.setTable("setting");
      for (let item in fileObj) {
        let formData = new FormData();
        formData.append("file", fileObj[item].file);
        let uploadResult = await sdk.uploadImage(formData);
        _data[item] = uploadResult.url;
      }
      const result = await sdk.callRestAPI(
        {
          id: id,

          setting_key: _data.setting_key,
          setting_value: _data.setting_value,
        },
        "PUT"
      );

      if (!result.error) {
        showToast(globalDispatch, "Updated");
        navigate("/admin/setting");
      } else {
        if (result.validation) {
          const keys = Object.keys(result.validation);
          for (let i = 0; i < keys.length; i++) {
            const field = keys[i];
            setError(field, {
              type: "manual",
              message: result.validation[field],
            });
          }
        }
      }
    } catch (error) {
      console.log("Error", error);
      setError("setting_key", {
        type: "manual",
        message: error.message,
      });
    }
  };
  React.useEffect(() => {
    globalDispatch({
      type: "SETPATH",
      payload: {
        path: "setting",
      },
    });
  }, []);

  return (
    <div className=" mx-auto rounded   p-5 shadow-md">
      <h4 className="text-2xl font-medium">Edit Setting</h4>
      <form className=" w-full max-w-lg" onSubmit={handleSubmit(onSubmit)}>
        <div className="mb-4  ">
          <label
            className="mb-2 block text-sm font-bold text-gray-700"
            htmlFor="setting_key"
          >
            Setting Key
          </label>
          <input
            placeholder="Setting Key"
            {...register("setting_key")}
            className={`focus:shadow-outline w-full appearance-none rounded border px-3 py-2 leading-tight text-gray-700 shadow focus:outline-none ${errors.setting_key?.message ? "border-red-500" : ""
              }`}
          />
          <p className="text-xs italic text-red-500">
            {errors.setting_key?.message}
          </p>
        </div>

        <div className="mb-4  ">
          <label
            className="mb-2 block text-sm font-bold text-gray-700"
            htmlFor="setting_value"
          >
            Setting Value
          </label>
          <textarea
            placeholder="Setting Value"
            {...register("setting_value")}
            className={`focus:shadow-outline w-full appearance-none rounded border px-3 py-2 leading-tight text-gray-700 shadow focus:outline-none ${errors.setting_value?.message ? "border-red-500" : ""
              }`}
            row={15}
          ></textarea>
          <p className="text-xs italic text-red-500">
            {errors.setting_value?.message}
          </p>
        </div>

        <button
          type="submit"
          className="focus:shadow-outline rounded bg-blue-500 px-4 py-2 font-bold text-white hover:bg-blue-700 focus:outline-none"
        >
          Submit
        </button>
      </form>
    </div>
  );
};

export default EditSettingPage;
