package main

import (
	"fmt"
	"learngo/retriever/mock"
	"learngo/retriever/real"
	"time"
)

type Retriever interface {
	Get(url string) string
}

type Poster interface {
	Post(url string, form map[string]string) string
}

const url = "http://www.baidu.com"

func download(r Retriever) string {
	return r.Get(url)
}

func post(poster Poster) {
	poster.Post(url, map[string]string{
		"name":   "ccmouse",
		"course": "golang",
	})
}

type RetrieverPoster interface {
	Retriever
	Poster
}

func session(s RetrieverPoster) string {
	s.Post(url, map[string]string{
		"contents": " 修改后的数据",
	})

	return "OK"
	//return s.Get(url)
}

func main() {
	var r Retriever
	r = mock.Retriever{"this is my test interface"}
	// 查询接口类型
	fmt.Printf("%T %v", r, r)
	inspect(r)
	//fmt.Println(download(r))
	r = &real.Retriever{
		UserAgent: "Mozilla/5.0",
		TimeOut:   time.Minute,
	}
	inspect(r)
	//fmt.Printf("%T %v" ,r,r )
	//fmt.Println(download(r))

	// type assertion  断言
	if retriever, ok := r.(mock.Retriever); ok {
		fmt.Println(retriever.Contents)
	} else {
		fmt.Println(" not a mock Retriever")
	}

	fmt.Println(" =================")
	retriever := mock.Retriever{"this is default value"}
	fmt.Println(session(retriever))
	fmt.Println(" ============= post ====")
	realretriever := &real.Retriever{UserAgent: "this is default Value"}
	fmt.Println(session(realretriever))
	fmt.Println(realretriever.UserAgent)

}

func inspect(r Retriever) {
	fmt.Printf("%T %v", r, r)
	switch v := r.(type) {
	case mock.Retriever:
		fmt.Println("Contents:", v.Contents)
	case *real.Retriever:
		fmt.Println("UserAgent:", v.UserAgent)
	}
}
