import { useAjax } from '@/Hook/useAjax'
import {
    LoginProps, loginAdApi, updateChannelInfoApi, updateChannelInfoPrpps, loginResultApi, allOfLoginUserApi,
    adsChannelAccountAdqApi, adsChannelAccountMpApi, adqPrpps, uploadLoginAdApi, AddAnd, addAndCreateApi, adsSharePageApi,
    getSharePageDetailApi, getSharePageApi, getShare, adsSharePageEditApi, allOfAccountApi, addPlanApi, adsPlan, getAdsPlanApi,
    adsPlanList, getAdsPlanLogApi, adsPlanLogList, getAdsPlanXqApi, adsChannelAccountAdqAllApi, adsChannelAccountMpAllApi, getUserInfoApi,
    setCopyPlanApi, CopyPlan, delBatchAjax, delLogBatchAjax, getAdsTableConfigApi, getUserConfigApi, updateUserConfigApi, allOfAccountGodsApi, 
    allOfAccountGodsDetailApi,delSharePageApi, allWechatInfoApi, allDataSourceApi
} from '@/services/operating/adAuthorize'
import { adqCollect, mpCollect } from '@/services/operating/adWeChat'
import moment from 'moment'
import { Dispatch, useReducer } from 'react'
import { CreatePlan } from '../../pages/launchSystem/launchManage/req'

let area = ["340100", "340200", "340300", "340400", "340500", "340600", "340700", "340800", "341000", "341100", "341200", "341300", "341500", "341600", "341700", "341800", "110000", "500000", "350100", "350200", "350300", "350400", "350500", "350600", "350700", "350800", "350900", "620100", "620200", "620300", "620400", "620500", "620600", "620700", "620800", "620900", "621000", "621100", "621200", "622900", "623000", "440100", "440200", "440300", "440400", "440500", "440600", "440700", "440800", "440900", "441200", "441300", "441400", "441500", "441600", "441700", "441800", "441900", "442000", "445100", "445200", "445300", "450100", "450200", "450300", "450400", "450500", "450600", "450700", "450800", "450900", "451000", "451100", "451200", "451300", "451400", "520100", "520200", "520300", "520400", "520500", "520600", "522300", "522600", "522700", "460100", "460200", "460300", "469001", "469002", "469003", "469005", "469006", "469007", "469021", "469022", "469023", "469024", "469025", "469026", "469027", "469028", "469029", "469030", "130100", "130200", "130300", "130400", "130500", "130600", "130700", "130800", "130900", "131000", "131100", "410100", "410200", "410300", "410400", "410500", "410600", "410700", "410800", "410900", "411000", "411100", "411200", "411300", "411400", "411500", "411600", "411700", "419001", "230100", "230200", "230300", "230400", "230500", "230600", "230700", "230800", "230900", "231000", "231100", "231200", "232700", "420100", "420200", "420300", "420500", "420600", "420700", "420800", "420900", "421000", "421100", "421200", "421300", "422800", "429004", "429005", "429006", "429021", "430100", "430200", "430300", "430400", "430500", "430600", "430700", "430800", "430900", "431000", "431100", "431200", "431300", "433100", "220100", "220200", "220300", "220400", "220500", "220600", "220700", "220800", "222400", "320100", "320200", "320300", "320400", "320500", "320600", "320700", "320800", "320900", "321000", "321100", "321200", "321300", "360100", "360200", "360300", "360400", "360500", "360600", "360700", "360800", "360900", "361000", "361100", "210100", "210200", "210300", "210400", "210500", "210600", "210700", "210800", "210900", "211000", "211100", "211200", "211300", "211400", "150100", "150200", "150300", "150400", "150500", "150600", "150700", "150800", "150900", "152200", "152500", "152900", "640100", "640200", "640300", "640400", "640500", "630100", "630200", "632200", "632300", "632500", "632600", "632700", "632800", "370100", "370200", "370300", "370400", "370500", "370600", "370700", "370800", "370900", "371000", "371100", "371200", "371300", "371400", "371500", "371600", "371700", "610100", "610200", "610300", "610400", "610500", "610600", "610700", "610800", "610900", "611000", "140100", "140200", "140300", "140400", "140500", "140600", "140700", "140800", "140900", "141000", "141100", "310000", "510100", "510300", "510400", "510500", "510600", "510700", "510800", "510900", "511000", "511100", "511300", "511400", "511500", "511600", "511700", "511800", "511900", "512000", "513200", "513300", "513400", "120000", "540100", "540200", "542100", "542200", "542400", "542500", "542600", "650100", "650200", "652100", "652200", "652300", "652700", "652800", "652900", "653000", "653100", "653200", "654000", "654200", "654300", "659001", "659002", "659003", "659004", "659005", "659006", "659007", "659008", "659009", "530100", "530300", "530400", "530500", "530600", "530700", "530800", "530900", "532300", "532500", "532600", "532800", "532900", "533100", "533300", "533400", "330100", "330200", "330300", "330400", "330500", "330600", "330700", "330800", "330900", "331000", "331100"]
interface State extends CreatePlan {

}
export const initData: State = {
    first: {
        pos_type: 999,
        product: {
            product_type: 'PRODUCTTYPE_WECHAT',
            product_id: '',
            product_info: '',
        },
        campaign: {
            cid: 0,
            ctype: 'CAMPAIGNTYPE_AUCTION', //购买方式 CAMPAIGNTYPE_AUCTION=竞价购买 CAMPAIGNTYPE_SCHEDULE=合约购买
            cname: '',
            can_mix_product: 1,
            end_time: 0,
            begin_time: moment().startOf('day').add(5, 'M').unix(),
            exposure_frequency: 8
        },
        sub_product: {
            subordinate_product_id: '',
            product_type: 'PRODUCTTYPE_WECHAT',
            product_id: '',
            spname: ''
        },
        target_groups: [
            {
                target_group: {
                    mp_conf: `{}`,
                },
                ad_groups: [{
                    ad_group: {
                        bid_strategy: 2, //优先跑量
                        aid: 0,
                        backup_aid: 0,
                        aname: '',  //广告名称 地域 性别 年龄组装
                        timeset: '',
                        product_id: "",//公众号appid
                        product_type: 'PRODUCTTYPE_WECHAT',
                        end_time: 0,
                        begin_time: moment().startOf('day').add(5, 'M').unix(),
                        strategy_opt: '{"bid_objective":7,"bid_action_type":7,"bid_objective_mode":1}',//"{\"bid_action_type\":1,\"bid_objective\":2,\"bid_objective_mode\":1}",
                        bid: 10, //出价26274 = 262.74
                        change_bid: 0,
                        budget: 100000, //每日预算 5010表示50.1
                        day_budget: 100000,//每日预算
                        contract_flag: 2,
                        pos_type: 999,
                        exposure_frequency: 8,
                        poi: "",
                        expand_targeting_switch: 'EXPAND_TARGETING_SWITCH_CLOSE', // 自动扩量
                        expand_targeting_setting: "[]",
                        cold_start_audience_id_list: '[]',
                        time_mode: 0,
                        multi_slot_type: 0,
                        auto_acquisition_switch: 0, //一键起量 0=关 1=开
                        auto_acquisition_budget: null, //探索预算
                        tracking_sourceid: ''
                    },
                    ad_target: {
                        mid: 0,
                        ad_behavior: '[{"in_action_list":[],"not_in_action_list":[101]}]',//in_action_list = 再营销 not_in_action_list = 排除营销，101 已关注你的公众号，301 曾领取你的微信卡券，401 曾对你的公众号广告感兴趣,501曾对你的朋友圈广告感兴趣,801已添加过企业微信
                        education: "[]",//学历"[\"DOCTOR\",\"MASTER\",\"BACHELOR\",\"JUNIOR_COLLEGE\",\"SENIOR\",\"JUNIOR\",\"PRIMARY\"]" 依次代表 博士，硕士，本科，专科，高中，初中，小学
                        device_price: "[]",//手机价格 "[\"1~14\",\"15~24\",\"25~34\",\"35~44\",\"45~100\"]" 依次代表1500元以下 1500~2500元 2500~3500元 3500~4500元 4500元以上
                        area: area, //地域
                        travel_area: "[]",
                        area_type: "area",
                        gender: JSON.stringify(['MALE']), //性别 MALE=男 FEMALE = 女 ""=全部
                        sns_optimal_user_group: "",
                        age: '["14~66"]', //年龄
                        device_brand_model: "[]",//手机品牌 定向正直1 排除负值-1
                        businessinterest: "[]",
                        app_behavior: "{}",
                        os: "[]",//手机系统 "[\"IOS\",\"ANDROID\"]"
                        marriage_status: "[]",
                        wechatflowclass: "[]",
                        connection: "[]",//联网方式 "[\"WIFI\",\"NET4G\",\"NET3G\",\"NET2G\"]"
                        telcom: "[]",//运营商 "[\"CTC\",\"CMC\",\"CUC\"]" CTC=电信 CMC=移动 CUC=联通
                        payment: "[]",
                        game_consumption_level: "[]",
                        custom_poi: "[]",
                        weapp_version: '{"min_ios_version":0,"min_android_version":0,"min_ios_os_version":"","min_android_os_version":""}',
                        oversea: "[]",
                        in_dmp_audience: "", //人群包
                        not_in_dmp_audience: "[]",
                        behavior_interest: "{}",
                        star_fans: "",
                    }
                }]
            }
        ],
        expected_ret: 0,
        materials: [{ tname: "", crt_size: 929 }],
    },
    second: {
        cid: '',
        pos_type: 999,
        page_name: '',//落地页名称
        materials: [{
            rid: '',
            cid: '',
            desc: '',
            link_hidden: 0,
            link_name: "阅读小说",
            is_show_friend: 0,
            show_first_comment: 0,
            interaction: 0,
            crt_size: 1480,
            tname: '',
            page_id: '',
            page_id_str: '',
            link_page_id: 2225950003,
            link_page_id_str: "0",
            page_type: 4,
            link_page_type: 4,
            dest_conf: "{}",
            ext_click_url: "",
            ext_exposure_url: "",
            dest_url: "",
            appmsg_info: "{}",
            title: "",
            scheme_url: "",
            is_hidden_comment: 0,
            crt_info: '',
            sns_dynamic_show_type: 0,
            sns_dynamic_show_text: "",
        }],
        product: {
            product_id: "",
            product_type: "PRODUCTTYPE_WECHAT",
        }
    },
}
export type Action = {
    type: 'setFirst' | 'setSecond' | 'setInit' | 'init',
    params: State
}
export function reducer(state: State, action: Action) {
    let { type, params } = action
    switch (type) {
        case 'setFirst':
            return { ...state, first: params?.first }
        case 'setSecond':
            return { ...state, second: params?.second }
        case 'setInit':
            return { ...state, ...params }
        case 'init':
            return { ...state, ...initData }
        default:
            return state;
    }
}


export default function useWeChat() {
    const [state, dispatch]: [state: State, dispatch: Dispatch<Action>] = useReducer(reducer, initData)

    const loginAd = useAjax((props: LoginProps) => loginAdApi(props), { formatResult: true })
    const uploadLoginAd = useAjax((props: LoginProps) => uploadLoginAdApi(props), { formatResult: true })
    const loginResult = useAjax((code: string) => loginResultApi(code), { formatResult: true })
    const updateChannelInfo = useAjax((props: updateChannelInfoPrpps) => updateChannelInfoApi(props), { formatResult: true })
    const allOfLoginUser = useAjax(() => allOfLoginUserApi(), { formatResult: true })
    const adsChannelAccountAdq = useAjax((props: adqPrpps) => adsChannelAccountAdqApi(props), { formatResult: true })
    const adsChannelAccountMp = useAjax((props: adqPrpps) => adsChannelAccountMpApi(props), { formatResult: true })

    const adsChannelAccountAdq1 = useAjax((props: adqPrpps) => adsChannelAccountAdqApi(props), { formatResult: true })
    const adsChannelAccountMp1 = useAjax((props: adqPrpps) => adsChannelAccountMpApi(props), { formatResult: true })

    const adsChannelAccountAdqAll = useAjax((channelId: number) => adsChannelAccountAdqAllApi(channelId), { formatResult: true })
    const adsChannelAccountMpAll = useAjax((channelId: number) => adsChannelAccountMpAllApi(channelId), { formatResult: true })

    const addAndCreate = useAjax((props: AddAnd) => addAndCreateApi(props), { formatResult: true })
    const adsSharePage = useAjax((props: any) => adsSharePageApi(props), { formatResult: true })
    const getSharePageDetail = useAjax((pageId: number) => getSharePageDetailApi(pageId), { formatResult: true })
    const getSharePage = useAjax((props: getShare) => getSharePageApi(props), { formatResult: true })
    const delSharePage = useAjax((props: string) => delSharePageApi(props), { formatResult: true })
    const adsSharePageEdit = useAjax((props: any) => adsSharePageEditApi(props), { formatResult: true })
    const allOfAccount = useAjax((props: any) => allOfAccountApi(props), { formatResult: true })
    const allWechatInfo = useAjax((props: any) => allWechatInfoApi(props), { formatResult: true })
    const allDataSource = useAjax((props: any) => allDataSourceApi(props), { formatResult: true })
    const addPlan = useAjax((props: adsPlan) => addPlanApi(props), { formatResult: true })
    const getAdsPlan = useAjax((props: adsPlanList) => getAdsPlanApi(props), { formatResult: true })
    const getAdsPlanLog = useAjax((props: adsPlanLogList) => getAdsPlanLogApi(props), { formatResult: true })
    const getAdsPlanXq = useAjax((planId: number) => getAdsPlanXqApi(planId), { formatResult: true })
    const getUserInfo = useAjax(() => getUserInfoApi(), { formatResult: true })

    // 删除
    const delBatch = useAjax((props: { taskIds: string }) => delBatchAjax(props), { formatResult: true, msgNmae: '删除' })
    const delLogBatch = useAjax((props: { logIds: string }) => delLogBatchAjax(props), { formatResult: true, msgNmae: '删除' })
    // 批量复制
    const setCopyPlan = useAjax((props: CopyPlan) => setCopyPlanApi(props))
    // 收藏
    const mpSc = useAjax((props: any) => mpCollect(props), { formatResult: true })
    const adqSc = useAjax((props: any) => adqCollect(props), { formatResult: true })


    const getAdsTableConfig = useAjax((props: { update: boolean }) => getAdsTableConfigApi(props), { formatResult: true })
    const getUserConfig = useAjax(() => getUserConfigApi(), { formatResult: true })
    const updateUserConfig = useAjax((props: {idList: string[], tableConfig: string}) => updateUserConfigApi(props), { formatResult: true })

    // 商品
    const allOfAccountGods = useAjax((props: any) => allOfAccountGodsApi(props), { formatResult: true })
    const allOfAccountGodsDetail = useAjax((props: any) => allOfAccountGodsDetailApi(props), { formatResult: true })

    return {
        getUserInfo,
        adsChannelAccountAdqAll,
        adsChannelAccountMpAll,
        getAdsPlanXq,
        getAdsPlanLog,
        loginAd,
        uploadLoginAd,
        updateChannelInfo,
        loginResult,
        allOfLoginUser,
        adsChannelAccountAdq,
        adsChannelAccountMp,
        adsChannelAccountAdq1,
        adsChannelAccountMp1,
        getAdsTableConfig,
        getUserConfig,
        updateUserConfig,
        addAndCreate,
        adsSharePage,
        addPlan,
        delSharePage,
        getSharePageDetail,
        getSharePage,
        adsSharePageEdit,
        allOfAccount,
        allOfAccountGods,
        allOfAccountGodsDetail,
        getAdsPlan,
        setCopyPlan,
        dispatch,
        state,
        mpSc,
        adqSc,
        delBatch,
        delLogBatch,
        allWechatInfo,
        allDataSource
    }
}