import request from "../utils/request";
import {Toast, showFailToast} from "vant";
import {Tongzhi, TongzhiStatuses, TongzhiQueryParam, PagedQueryParam} from "~/types";
import useUser from "./user";
import { reactive } from "vue";
import useLoading from "~/store/loading";

interface TongzhiStore {
    [TongzhiStatuses.Unread]: {
        list: Tongzhi[],
        total: number,
        pages: number,
    },
    [TongzhiStatuses.Read]: {
        list: Tongzhi[],
        total: number,
        pages: number,
    }
    current: Tongzhi
}

const store = reactive<TongzhiStore>({
    [TongzhiStatuses.Unread]: {
        list: [],
        total: 0,
        pages: 0
    },
    [TongzhiStatuses.Read]: {
        list: [],
        total: 0,
        pages: 0
    },
    current: {} as Tongzhi
});

export default function useTongzhi() {

    const usingUser  = useUser();
    const {load_start, load_done} = useLoading();

    async function query(params: PagedQueryParam, tongzhi_status: TongzhiStatuses) {
        let role = usingUser.store.currentRole;
        let role_id = usingUser.store.current.roles.find(r => r.role_key === role)?.hty_role_id;
        
        let data: TongzhiQueryParam = {
            ...params, role_id, tongzhi_status
        }
        const {r, d, e} = await request({url: '/api/v1/uc/find_tongzhis',data});
        if (r) {
            if (params.page === 1) {
                store[tongzhi_status].list = [];
            }
            let [list, pages, total] = d;
            store[tongzhi_status].list = [...store[tongzhi_status].list, ...list];
            store[tongzhi_status].total = total;
            store[tongzhi_status].pages = pages;
            if (tongzhi_status === TongzhiStatuses.Unread) {
                usingUser.store.current.unread_tongzhi_count = total
            }
        } else {
            showFailToast(e);
        }
        return Promise.resolve(r);
    }

    async function setRead(tongzhi_id: string) {
        const {r, d, e } = await request({url: '/api/v1/uc/update_tongzhi_status_by_id', method: 'POST', data: { tongzhi_id, tongzhi_status: TongzhiStatuses.Read }});
        if (r) {
            usingUser.getUnreadTongzhis();
            query({page: 1, page_size: 20}, TongzhiStatuses.Unread)
            query({page: 1, page_size: 20}, TongzhiStatuses.Read)
        } else {
            showFailToast(e)
        }
        
    }

    async function read(id: string) {
        load_start()
        const {r, d, e} = await request({url: '/api/v1/uc/find_tongzhi_by_id/' + id})
        if (r) {
            store.current = d;
        } else {
            showFailToast(e)
        }
        load_done()
    }


    async function setReadAll() {
        load_start()
        let role = usingUser.store.currentRole;
        let role_id = usingUser.store.current.roles.find(r => r.role_key === role)?.hty_role_id;
        let data: TongzhiQueryParam = {
            role_id, user_id: usingUser.store.current.hty_id
        };
        const { r, d, e } = await request({url: '/api/v1/uc/clear_all_unread_tongzhis_by_user_id_and_role_id', data});
        if (r) {
            // clear shown list
            store.Unread.list = []
            store.Unread.total = 0;
            store.Unread.pages = 0;
            usingUser.getUnreadTongzhis();
            query({page: 1, page_size: 20}, TongzhiStatuses.Unread)
            query({page: 1, page_size: 20}, TongzhiStatuses.Read)
        } else {
            showFailToast(e)
        }
        load_done()
    }


    async function remove(tongzhi_id: string, tongzhi_status: TongzhiStatuses) {
        load_start()
        const { r, d, e } = await request({url: `/api/v1/uc/delete_tongzhi_by_id/${tongzhi_id}`, method: "POST"});
        if (r) {
            if (tongzhi_status === TongzhiStatuses.Unread) {
                usingUser.getUnreadTongzhis();
            }
            query({page: 1, page_size: 20}, tongzhi_status)
        } else {
            showFailToast(e)
        }
        load_done()
    }


    async function removeAll(tongzhi_status: TongzhiStatuses) {
        load_start()
        let role = usingUser.store.currentRole;
        let role_id = usingUser.store.current.roles.find(r => r.role_key === role)?.hty_role_id;
        let data: TongzhiQueryParam = {
            role_id, tongzhi_status, user_id: usingUser.store.current.hty_id
        };
        const { r, d, e } = await request({url: '/api/v1/uc/delete_all_tongzhis_by_status_and_role_id_and_user_id', data});
        if (r) {
            if (tongzhi_status === TongzhiStatuses.Unread) {
                usingUser.getUnreadTongzhis();
            }
            query({page: 1, page_size: 20}, tongzhi_status)
        } else {
            showFailToast(e)
        }
        load_done()
    }


    return {
        store, query, setRead, setReadAll, remove, removeAll, read
    }
}
